适配器不与listview一起使用

时间:2014-12-16 11:23:47

标签: android listview imageview adapter

我想在listView中显示一些图标。 这是适配器

public class IconAdapter extends BaseAdapter {
    private int[] iconList;
    private Context context;
    public IconAdapter(int[] iconList, Context contex) {
        this.iconList = iconList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        ImageView mImageView = new ImageView(context);
        mImageView.setBackgroundResource(iconList[position]);
        return mImageView;
    }
}

这就是我设置适配器的方式

int[] iconList = new int[]{R.drawable.ic_action_content_new, R.drawable.ic_color_lens_black_48dp};
holder.mListView.setAdapter(new IconAdapter(iconList, context));

问题是listView没有显示任何内容

2 个答案:

答案 0 :(得分:2)

你的getCount()方法返回0 ...

更改为:

    @Override
    public int getCount() {
        return iconList.length;
    }

    @Override
    public Object getItem(int position) {
        return iconList[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }   

答案 1 :(得分:1)

更改您的代码,

@Override
public int getCount() {
    return iconList.length;
}

@Override
public Object getItem(int position) {
    return iconList[position];
}