cannot resolve method getText() - android

I'm new to Android developing and I'm trying to create a TODO list similar to an online tutorial I'm following.
Everything but "todoText.setText(todoList.get(position).getText());" compiles correctly.
With that line, I get the following error message: "cannot resolve method getText()."
This is part of a class responsible for displaying each list row in a TODO checklist:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
TextView todoText = (TextView) rowView.findViewById(R.id.todoText);
todoText.setText(todoList.get(position).getText());
return rowView;
}
I have no idea how to fix this. Any help would be greatly appreciated. Thank you.

Hard to say without seeing todoList declaration, but chances are that todoList is a List of Strings (or something else but a TextView). If that's the case, you may just need to do.-
todoText.setText(todoList.get(position));

I have no idea about the contents of your todoList but you can do a workaround if the content is not a String by using .toString()
Try to change the code into -
todoText.setText(todoList.get(position).toString());
hope it works.

Related

findViewById returns null in getView of adapter

Im trying to understand why findViewById always returns null when Im reusing my view in getView() of my custom adapter.
1) Below image shows my ListView. Pretty basic.
2) I have defined another xml file called selected_apps_button.xml which is view that'll be added to the ListView above. Here is the image
3) Here is how im inflating the view in step 2 to add to the ListView in step 1
public View getView(final int pos, View convertView, ViewGroup parent) {
View selectAppsButton = convertView;
if(selectAppsButton == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
selectAppsButton = inflater.inflate(R.layout.selected_apps_button, parent, false);
}
final TileButton appColorButton = (TileButton) selectAppsButton.findViewById(R.id.appColorr);
final TileButton appNameButton = (TileButton) selectAppsButton.findViewById(R.id.appNamee); //appNameButton is always null
4) In the above code 'appNameButton' is always null. Interesting thing is 'appColorButton' from same inflated layout is NOT null.
5) Any idea what's going on?
I have build and rebuild the project like a million times. But it didnt help.
Please let me know if more information is required. Thanks.

Different components in different rows of an Android ListView

I am new to Android. I would like to create an Activity very similar to "Add event" part of Android Calendar app. To me it looks like a ListView with different components in each row. I could be wrong. If I am right, I still don't know how to add different components to each row of a ListView, e.g., EditText in one row, TextView in another row, etc. If this app is not ListView at all, if anybody can tell me how I can create something similar, I'd appreciate that a lot.
hello check this link it be helpful 1
listView with different component
First you have to learn how to implement a custom Adapter (see this tutorial: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown)
Then in your getView overriden method (you'll learn about it in the tutorial) you have to do something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = null;
if (position == LAYOUT1_POSITION) //In this row you will place the layout named layout1
rowView = inflater.inflate(R.layout.rowLayout1, parent, false);
if (position == LAYOUT2_POSITION)
rowView = inflater.inflate(R.layout.rowLayout2, parent, false);
//Do similar for all your different layouts
return rowView;
}

overriding Adapter.getView

I'm new to android programming and doing the first steps with Adapters (for a ListView).
Overriding the Adapter.getView I often see things like this:
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.table_row, null);
} else {
itemView = convertView;
}
// play with itemView
return itemView;
}
My question is what speaks against this:
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = super(position, convertView, parent);
// play with itemView
return itemView;
}
This seems to work for me but I'm sure there's a big point I'm missing :D
Thanks for reading and sorry for my bad english ...
You can use
View itemView = super(position, convertView, parent);
if only you are deriving from "ready to use" adapters (not BaseAdapter), like SimpleAdapter, or ArrayAdapter, as they already have their implementation for the getView().
Have a look at them: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/ArrayAdapter.java#361 for the ArrayAdapter, and
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/SimpleAdapter.java#113 for SimpleAdapter.
If you derive from BaseAdapter, you will have to manualy implement the whole method, as you've described in the first example, because it does not have it out of the box: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/BaseAdapter.java#BaseAdapter
The getView(..)-method of the Adapter can be multiple ways. The only question is, which one is the most efficient?
An interesting article to read and make you understand the ListView more detailed: http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
If you mean that this piece of code:
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.table_row, null);
} else {
itemView = convertView;
}
seems unnecessary for you: this piece of code allows Android to create a relatively small number of cells (equals to the number of cells that are visible on your screen +-), and then "recycle" these cells - use them over and over again while the user scrolls the list, instead of creating a cell for each object in your array.
This will help you with:
Saving memory - because you don't create view for each element in your array
Saving CPU usage - creating a view object out of xml file ("inflating") is relatively expensive task and doing so for each item in your array might choke your UI thread

Unable to use LayoutInflater in custom adapter

I'm looking into writing a custom adapter to populate a listview with 3 textviews per line. I've found quite a bit of example code to do this, but the one that seemed the best was at: http://www.anddev.org/custom_widget_adapters-t1796.html
After a few minor tweaks to fix some compiler issues with the latest Android SDK, I got it running, only to get the exception:
ERROR/AndroidRuntime(281): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
So I did a lot of research and found lots of possible reasons and fixes for this. None of which changed a thing. My adapter code is currently:
public class WeatherAdapter extends BaseAdapter {
private Context context;
private List<Weather> weatherList;
public WeatherAdapter(Context context, int rowResID,
List<Weather> weatherList ) {
this.context = context;
this.weatherList = weatherList;
}
public int getCount() {
return weatherList.size();
}
public Object getItem(int position) {
return weatherList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Weather weather = weatherList.get(position);
//LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.weather_row, null, true);
TextView cityControl = (TextView)v.findViewById( R.id.city );
TextView temperatureControl = (TextView)v.findViewById( R.id.temperature );
ImageView skyControl = (ImageView)v.findViewById( R.id.sky );
return v;
}
}
So I have tried the commented out way of getting the inflater, and the currently uncommented out. I have tried passing "parent" to inflate as well as null, and passing "true", "false" and omitting completely the last parameter. None of them have worked, and all examples I've found so far have been from 2008 which I get the feeling are a bit outdated.
If anyone could help with this then I would love to resolve the issue.
I believe this line is at fault:
View v = inflater.inflate(R.layout.weather_row, null, true);
You need instead:
View v = inflater.inflate(R.layout.weather_row, parent, false);
The false makes the inflated view independent of the parent, not attached to it, which very oddly seems to be the accepted design for custom views within AdapterViews. Why this is so, I find utterly baffling, but the pattern above worked for me.
I'm a beginner also so take this answer with a pinch of salt - if it doesn't work, move on and Google some more. Not familiar with AdapterView since I traditionally have a ListView or GridView and a custom Adapter extended off a BaseAdapter and then listView.setAdapter(myCustomAdapter).
You could try making something like this inside the WeatherAdapter class:
public void addToList(Weather mWeather) {
weatherList.add(mWeather);
}
Then in the class that calls WeatherAdapter:
weatherAdapter.addToList(weatherToAdd);
weatherAdapter.notifyDataSetChanged();
Also you need to optimize it more in the getView method:
http://www.youtube.com/watch?v=wDBM6wVEO70
For the AdaptertView addView method:
void addView(View child)
This method is not supported and
throws an
UnsupportedOperationException when
called." (From Android documentation)
Probably the inflating procedure calls the addView method and this is not possible from an AdapterView, or its AdapterView.
From the documentation:
"An Adapter object acts as a bridge
between an AdapterView and the
underlying data for that view. The
Adapter provides access to the data
items. The Adapter is also responsible
for making a View for each item in the
data set".
I think that the inflating operation could be done from a simple Activity that models your view and all other operations, for example, retrieving data and showing data in other classes.
Hope it will be helpful!
#Axel22's answer is key, but there are a few other things missing from your code. First, you should be extending either BaseAdapter or ArrayAdapter, depending on your preference. Second, you want to get in the practice of using a ViewHolder to avoid making excessive calls to findViewById, and (most importantly) recycling your View.
private Class ViewHolder {
public TextView cityControl;
public TextView temperatureControl;
public ImageView skyControl;
public ViewHolder(TextView cityControl, TextView temperatureControl, ImageView skyControl) {
this.cityControl = cityControl;
this.temperatureControl = temperatureControl;
this.skyControl = skyControl;
}
Your getView function can recycle views and utilize the ViewHolder class as follows:
public View getView(int position, View convertView, ViewGroup parent) {
Weather weather = weatherList.get(position);
// This is how you attempt to recycle the convertView, so you aren't
// needlessly inflating layouts.
View v = convertView;
ViewHolder holder;
if (null == v) {
v = LayoutInflater.from(getContext()).inflate(R.layout.weather_row, parent, false);
TextView cityControl = (TextView)v.findViewById( R.id.city );
TextView temperatureControl = (TextView)v.findViewById( R.id.temperature );
ImageView skyControl = (ImageView)v.findViewById( R.id.sky );
holder = new ViewHolder(cityControl, temperatureControl, skyControl);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.cityControl.setText("Metropolis");
holder.temperatureControl.setText("78");
holder.skyControl.setImageResource(R.drawable.daily_planet);
return v;
}
For tons more examples (and other optimization tips), see this blog post (or just google for ViewHolder).

Problem in Listview Custom adapter class

I am explaining my issue straightaway. Please refer the following code snippet.
#Override
public View getView(int index, View convertView, ViewGroup parent) {
Comments comment = comments.get(index);
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.comment_row, null);
}
TextView tvAuthor = (TextView) convertView.findViewById(R.id.commentAuthor);
System.out.println("tvAuthor"+tvAuthor);
tvAuthor.setText(comment.getAuthor());
convertView.setTag(comment);
return convertView;
}
I get null for tvAuthor . As a result, in the immediate next line where I try to setText, I get null pointer exception.
I have declared commentAuthor in the xml correctly. I cannot trace, from where this error pops up. Experts, kindly help.
ny help in this regard is well appreciated.
Look forward,
Regards,
Rony
Make sure that View.findViewById() is called after View.onCreate() since this is when XML is parsed.
Preferably you'd put .findViewById() inside .onCreate() and save the result in a field for use elsewhere.
I think the problem is in the line:
System.out.println("tvAuthor"+tvAuthor);
As you have declared tvAuthor as TextView, this variable refers to the textview, not its value, so while displaying value from the textview to the console, you needs to write:
if(! tvAuthor.getText.toString.equalsIgnoreCase(""))
{
System.out.println("tvAuthor"+ tvAuthor.getText.toString());
}
so that it will take a value from the tvAuthor TextView and then display on the console.

Categories

Resources