views:

144

answers:

2

I am trying to create a ListView that will be populated with the entries from an array.

So this is my item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dip" >  

    <ImageView android:id="@+id/list_item_image"
        android:layout_height="wrap_content"
        android:padding="2dip" 
        android:layout_gravity="center_vertical|center_horizontal" 
        android:layout_width="50dip"/>  
    <TextView android:id="@+id/list_item" 
        android:layout_height="fill_parent"
        android:textSize="25sp"
        android:layout_width="fill_parent" 
        android:ellipsize="marquee" 
        android:gravity="center_vertical" 
        android:padding="5dip" >
    </TextView>
</LinearLayout>

I tried changing the layout_height of the LinearLayout but I ran into some problems. If I keep the height at wrap_content, my list is displayed with the correct entries -- Item 1, Item 2, Item 3, and so on until Item 12. However if I change the height to 60dip, the entries repeat after the sixth entry (I get Item 1, Item 2, Item 3, Item 4, Item 5, Item 6, Item 1, Item 2, Item 3...). If I keep on making it larger, the entries repeat more frequently.

This is a snippet from the ListAdapter where I set the list entries:

public View getView(int position, View convertView, ViewGroup parent) {

    LinearLayout layout;

    if (convertView == null){
         layout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.items_list_item, parent, false);

         TextView title = (TextView) layout.findViewById(R.id.list_item);
         title.setText(menuItems[position]);

         ImageView icon = (ImageView) layout.findViewById(R.id.list_item_image);
         int logo = getResources().getIdentifier(menuIcons[position], "drawable", getPackageName());
         icon.setImageResource(logo);

    } else {
         layout = (LinearLayout) convertView;
    }
  return layout;
}

Anybody else encountered this problem? I do not understand what is going on since I thought it should be straight-forward grabbing from the array.

EDIT: included the whole of my getView() method. Pardon the ugly way of getting the icons, I haven't figured it out yet,

+1  A: 

From the little snippet of code I'm going to guess that it's something to do with the views being reused and the text not getting updated. I'm not certain though without seeing all of the code for the ListAdapter.

Take a look at this session from Google I/O 2010 for loads of really helpful information on how to use ListViews (and by extension adapters). It contains lots of tips and advice on the best way of using them. If you have time watch the video, if not the slides are avaliable.

Good Luck :)

matto1990
+1 for the link to the I/O session. Great session.
Juri
It is, makes me want to go next year. Think it's in the middle of my exam period normally though :(
matto1990
Thanks for the link, will watch it later.
Zarah
+1  A: 

You didn't post enough code, but in your Adapter's getView(...) try to make use of the convertView.

public View getView(int position, View convertView, ViewGroup parent){
   if(convertView == null){
      convertView = mInflater.inflate(R.layout.my_listitem_row, parent, false);
   }

   //...fill the TextViews on your layout

   return convertView;
}

Fetching the icons should be as easy as

icon.setImageResource(R.drawable.my_icon); //the res/drawable folder has the my_icon.png file
Juri
Posted the whole code above. Seems like moving the setting of the TextView and the ImageView to outside of the `if-else` did the trick. Thanks!
Zarah
Modified my post to include how you can set your icons.
Juri
Thanks Juri! With regards to the icon, I don't know how to put R.drawable.* in an int array in strings.xml so that I can just pull the contents like what I did for the strings (String[] menuContents = getResources().getStringArray(R.array.menu_items);).I think that is a different topic altogether, and someone might scold us. :D
Zarah