i'm trying to set textView in custom row view of listView like below but the app crashes returning null pointer exception
TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
txtrow.setText("text");
but when i put it under setOnClickListener it works
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
txtrow.setText("text");
}
});
You should be doing this inside your ListView's adapter. Whatever data you are using the back your adapter should contain the text you want so that you can set it on the appropriate TextView when you actually create the row view in getView().
It's important to note that getChildAt(index) returns the child at the given index from the ListView's current children. In other words, getChildAt(0) does not necessarily give you the list item at position 0. The user may have scrolled the list so that it now shows data from positions 10 through 15 (for example), in which case getChildAt(0) gives you the view for position 10, not position 0.
To set text in custom row in listview
Please follow the following points
1. custom arrayadapter by extending Arrayadpater (for simple)
2. and overwrite getview as following
#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.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
return rowView;
}
3. and set the adapter to the listview
And for more detial please refer this site
[http://www.vogella.com/tutorials/AndroidListView/article.html][1]
Related
Hello guys I want to check through my GridView and see if any of the checkBox is checked or not. Eventually, I want to delete the rows in my database and update my custom adapter but I'm lost on what function to use or how to approach it. I know to use some kind of while/for loop but which data type can surf and iterate each list items? I was looking around and .getChildAt(index) seems like something that would be useful but it doesn't work/appear for my custom adapter variable in my MainActivity.
Here is my Custom Adapter class for reference
//variable responsible for making checkbox visible or not
private boolean displayCheckBox;
//constructor - it takes the context and the list of words
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
}
//sets the visibility of the checkBox
public void setCheckBoxVisibility(boolean visible){
this.displayCheckBox = visible;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
}
//getting the checkBox view id
CheckBox checkBox = (CheckBox) listItemView.findViewById(R.id.check_box);
checkBox.setVisibility(displayCheckBox ? View.VISIBLE : View.GONE);
//Getting the current word
WordFolder currentWord = getItem(position);
//making the 3 text view to match our word_folder.xml
TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);
TextView title = (TextView) listItemView.findViewById(R.id.title);
TextView desc = (TextView) listItemView.findViewById(R.id.desc);
//using the setText to get the text and set it in the textView
date_created.setText(currentWord.getDateCreated());
title.setText(currentWord.getTitle());
desc.setText(currentWord.getTitleDesc());
return listItemView;
}
}
The get view method from the script below crashes everytime I ran the app (the error points to that method ) this is the error :
07-11 17:25:01.147 8512-8512/com.example.android.quakereport E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.android.quakereport, PID: 8512
android.content.res.Resources$NotFoundException: Resource ID #0x0
and this is the ArrayAdapter from the getview method code :
public class EarthQuakeAdapter extends ArrayAdapter<Earthquake> {
public EarthQuakeAdapter(Activity context, ArrayList<Earthquake> Earthquakes) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, Earthquakes);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
return super.getView(position, convertView, parent);
}
Earthquake currentEarthquake = getItem(position);
TextView magnitude = (TextView) listItemView.findViewById(R.id.magnitude);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
magnitude.setText(Earthquake.getmMagnitude());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView place = (TextView) listItemView.findViewById(R.id.Place); // Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
place.setText(Earthquake.getMplace());
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
TextView date = (TextView) listItemView.findViewById(R.id.Date);
date.setText(Earthquake.getmDate());
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
I can add any code or ressource if needed just tell me on the comments.
You're never really returning the ViewHolder you inflated.
You shouldn't call super when the view is null. You should inflate it, get the child views, populate them and finally return the inflated root view.
Take a look at the docs to learn more about this process.
Try to write the adapter like this:
public class EarthQuakeAdapter extends ArrayAdapter<Earthquake> {
// ...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Earthquake currentEarthquake = getItem(position);
TextView magnitude = (TextView) listItemView.findViewById(R.id.magnitude);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
magnitude.setText(Earthquake.getmMagnitude());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView place = (TextView) listItemView.findViewById(R.id.Place); // Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
place.setText(Earthquake.getMplace());
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
TextView date = (TextView) listItemView.findViewById(R.id.Date);
date.setText(Earthquake.getmDate());
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
You can read more about smooth scrolling here on android's training page.
And as an option, you could switch to RecyclerView, you can read more about it here.
You shouldn't need to call super.getView(...) in the overridden getView function, and you're passing in an invalid resource ID. Simply remove that line, since you're not using its return value anyway:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
...
}
I have a listview that contains items that have listviews. I am trying to populate item's listviews from inside the getView of the custom adapter that populates the "parent" listview:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
System.out.println("session adapter: here1");
if (v == null) {
LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_session, null);
}
SessionObject i = sessions.get(position);
if (i != null) {
tvTrackName = (TextView) v.findViewById(R.id.textViewTrackName);
tvTrackName.setText(i.trackName);
tvSessionName = (TextView) v.findViewById(R.id.textViewSessionName);
tvSessionName.setText(i.sessionName);
tvSessionModerator = (TextView) v.findViewById(R.id.textViewModeratorName);
tvSessionModerator.setText("Moderator: "+i.sessionModerator);
listAbstracts = i.abstractList;
lvAbstracts = (ListView) v.findViewById(R.id.listViewAbstracts);
AbstractObjectAdapter adapter = new AbstractObjectAdapter(act, R.id.listViewAbstracts, listAbstracts);
lvAbstracts.setAdapter(adapter);
}
return v;
}
The "interior" adapter seems to only be calling its getView function once, regardless of the number of items in its list. If there are 2 items, only the first gets put into the listview. Is this the wrong way to do this? Am I missing something?
Any help is appreciated. Thanks.
Just create your item view dynamically, adding textviews to a linearlayout. You'll just have to be careful about handling the convertView, initially it will be easiest to always ingore it but you'll take a bit of a performance hit.
Optimise later.
I have an app that already many activities and class. Like Profile, Friends, Photos, News Feed, etc. I want to integrate that sliding drawer into my main activities. in the examples, they are just showing array list which is implement onClick. so my question is how to change that array list to become my activities like friends, profile, home and etc.
You can use ListView with custom adapter. to adjust custom adapter you can link in your listView that adapter. and in adapter you can put custom icon
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflator.inflate(R.layout.row, parent, false);
ImageView icon = (ImageView) rowView.findViewById(R.id.row_icon);
TextView title = (TextView) rowView.findViewById(R.id.row_title);
title.setText(proMenu[position]);
String s = proMenu[position];
if(s.equalsIgnoreCase("Home")){
icon.setImageResource(R.drawable.icon_home);
}
else
if(s.equalsIgnoreCase("Best Nearby")){
icon.setImageResource(R.drawable.specialss);
}
I just set up my ArrayAdapter to convertView to my favor , but now I wonder how to control the children of the LinearLayouts am using in that listview , lets say I have a TextView in the Linearlayout , I want to call setText , but I don't know how , or maybe I have a button which I want to call setOnClickListener , I don't know how !
Here is my getView code :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null){
convertView = inf.inflate(R.layout.normal_row, parent , false);
}
switch(position){
case(0):
// here is where my first LinearLayout goes , containing an Image and Switch which I need to program in onCheckedChangeListener ...
convertView = (LinearLayout) inf.inflate(R.layout.row_switch, parent,false);
break;
case(1)
// here I need to set the Text for the following two options on some basis , they are both linearLayouts containing a single textView which I want to call setText for each case 1 and 2.
break;
case(2)
}
TextView textview = (TextView) convertView.findById(R.id.txt);
text.setText("textview");
Button btn = (Button) convertView.findById(R.id.btn);
btn.setTag(textview);
btn.setOnClickListener(...)
{
onClick(..){
TextView t = (Textview)btn.getTag():
t.setText("new value")
}
}
i suggest you to look at the question which i replied before.
Let's say your normal_row layout has a TextView in it with the id #id/my_text_view. Then you would set the text like
TextView view = (TextView) convertView.findViewById(R.id.my_text_view);
view.setText("Some text");
You can access anything in the layout you've inflated by its id in a similar way.
One thing to note though is there seem to be two types of views that you're inflating. Let's say that you try to use the code I gave you, but my_text_view is only in normal_row. Then if you've inflated the row_switch view, the findViewById() will return null because it can not find a view with the given id.
Use onItemClick and find your view by the view that the OnItemClickListener provides you!
For example: `mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});`
By the passed View find your textview or your button inside every listView children.
Example: TextView tempTextView = (TextView) arg1.findViewById(R.id.myTextView);
The id of the textview is the one you assigned to it in your inflated layout for every row!