Android GridView Adapter显示空白视图

时间:2014-02-11 21:00:00

标签: android gridview imageview adapter

我需要将我的GridView中的项目作为单独的对象,可以重新绘制自己,例如在下载他们所代表的游戏时显示进度条。所以我为View创建了一个包装器。 问题是网格项根本不显示。 为简单起见,我只在LibraryAlbumView中添加了一个图像 如果我setBackgroundColor(0xFF00FF00);我在网格单元格中看到了这个绿色背景,而不是其他任何东西。 在我的包装中:

public class LibraryAlbumView extends LinearLayout {
private Context context;
public LibraryAlbumView(Context context) {
    super(context);
    this.context = context;
    ImageView image;
    image = new ImageView(context);
    image.setImageDrawable(context.getResources().getDrawable(
            R.drawable.delete));
    this.addView(image);

}

我的适配器代码是:

public class LibraryAlbumAdapter extends BaseAdapter {
private Context context;

public LibraryAlbumAdapter(Context context) {
    this.context = context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        gridItem = new LibraryAlbumView(context);
        return gridItem;
            } else {
            return (LibraryAlbumView) convertView;
    }

但是,如果我将包装器更改为LibraryAlbumView extends ImageView 并在其构造函数中我改为:

            super(context);
    this.context = context;
    setImageDrawable(context.getResources().getDrawable(
            R.drawable.delete));

我确实看到那里的图像

1 个答案:

答案 0 :(得分:0)

看起来是因为你没有覆盖适配器中的其他重要方法,如getCount()。由于Adapter的默认实现返回零,因此GridView认为您没有要显示的项目。

除上述信息外,您似乎没有为LayoutParams设置ImageView,因此LinearLayout不知道如何展示它并选择提供零宽度和高度,这可以解释为什么你看到绿色背景,没有别的。

相关问题