user782104
user782104

Reputation: 13555

How to add e.g. 5 elements to a 10 rows listview in android?

I am previously working on PHP and js , and recently I am working on android listview However, I encountered a problem in creating a custom adapter for listview

public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO
        if (arg1 == null) {
            arg1 = myInflater.inflate(R.layout.grid, arg2, false);
        }
        TextView name = (TextView) arg1.findViewById(R.id.text1);
        TextView desc = (TextView) arg1.findViewById(R.id.text2);
        ImageView image = (ImageView) arg1.findViewById(R.id.image1);
        if (arg0 < images.length) {
            image.setImageResource(images[arg0]);
        }
        name.setText(names[arg0]);
        desc.setText(description[arg0]);
        return arg1;
    }

The problem is I have 3 array of content to pass to the listview grid, for the first two array, there are 10 element and the last one have 5 only. So , it is out of boundries for the last one. I added a condition to check whether it exceed 5 , but args0 seems not increased according to the row?

if (arg0 < images.length) {
                image.setImageResource(images[arg0]);
            }

The first five row and some other rows also has image setted, why is that and how to fix this? thanks

Upvotes: 0

Views: 137

Answers (2)

Christian R.
Christian R.

Reputation: 1538

In General

since you want to display Data to your list, plx create an Object that represents Data.

like you named in your comment above:

public class ListEntry {
  String name = "";
  String gender = "";//use enum here perhaps -.-
  String photoUrl = null; //or use byte[] photo or whatever you've stored in your array before
  // write getters/setters for your members
}

then you can use one array ListEntry[] (or List<ListEntry>) to access all data. this way you get around your indexOutOfBoundsException.

lookup any listadapter tutorials online, e.g. the one from Vogella

Why do more than the first five entries have an image?

Androids Adapters for Listviews implement a caching mechanism to reduce the inflating (performance/memory cost intensive) of new list-items (e.g. rows) to a minimum. therefore there are only as many rows (or little more) created as displayed by the list. since you only set images if there are any, but never remove already set images from rows, you result in some rows that replay images they shouldn't. these rows are cached from previously outscrolling rows.

therefore add something like

if (listItem.photo != null) {
  image.setImageResource(images[arg0]);
} else {
  image.setVisibility(View.GONE);
}

as reference for listviews and their caching mechanism see Romain Guy on ListViews

Edit Regarding usage of Listadapter

The getView(..) you posted above is inside your ListAdapter implementation, prefarrably you've extended an ArrayAdapter<T>. if so, your T should now state ListEntry and you have any line of code that states

MyArrayAdapter myAdapter = new MyArrayAdapter() or something like that.

now you have an array or List of ListEntry like List<ListEntry> myCollection = new ArrayList<ListEntry>() or ListEntry[] listEntries = new ListEntry[10] and use

myAdapter.addAll(listEntries);

to get an item of your list inside your getView(..) you can use:

ListEntry currentEntry = getItem(arg0);

and refer the single members of currentEntry to set them ;-)

Upvotes: 1

Romain Pellerin
Romain Pellerin

Reputation: 2480

What about

if (images[arg0] != null) image.setImageResource(images[arg0]);

?

Upvotes: 0

Related Questions