0

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]);
        // name.setText(arg0);
        // desc.setText(images.length);
        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 3 only. So , it is out of boundries for the last one. I added a condition to check whether it exceed 3 , but args0 seems not increased according to the row?

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

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

4

1 回答 1

1

首先,正确地使用“image.length()”而不是“image.length”。

其次,如果您检查 Android Developer for ImageView,您会发现 ImageView 没有像 .length() 这样的方法。

我的第一个猜测是,您将包含您的值的 ArrayList 与您的 ImageView 混合在一起,这只是为了显示目的。

于 2013-07-24T11:32:56.017 回答