【问题标题】:Android BaseAdapter, return item idAndroid BaseAdapter,返回项目id
【发布时间】:2013-12-12 10:54:48
【问题描述】:

我正在尝试获取它,以便在显示图像 1 时,文本视图会显示其名称

这是我的名字数组

String[] soundcloudDates = { 
    "Image 1", 
    "Image 2", 
    "Image 3", 
    "Image 4",
};

这是我使用来自 GitHub 的 EcoGallery 库的 BaseAdapter 所有图像都正确滚动,但其 id 的输出到处都是 通常它开始打印位置 = 1;,然后直到位置 = 3; 才改变;

    private class ImageAdapter extends BaseAdapter {
    private Context context;

    ImageAdapter(Context context) {
        this.context = context;
    }

    public int getCount() {
        return 4;
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        // Not using convertView for simplicity. You should probably use it
        // in real application to get better performance.
        ImageView imageView = new ImageView(context);
        int resId;
        switch (position) {
        case 0:
            resId = R.drawable.logo;
                            position = 0;
                            dateView.setText(soundcloudDates[position]);


            break;
        case 1:
            resId = R.drawable.soundcloudtest;
                            position = 0;
                            dateView.setText(soundcloudDates[position]);


            break;
        case 2:
            resId = R.drawable.logo;
                            position = 0;
                            dateView.setText(soundcloudDates[position]);


            break;
        case 3:
            resId = R.drawable.ic_launcher;
                            position = 0;
                            dateView.setText(soundcloudDates[position]);

            break;
        default:
            resId = R.drawable.ic_launcher;
        }
        imageView.setImageResource(resId);
        return imageView;
    }
}

我尝试将 setText() 放在不同的地方没有效果。

我不明白我做错了什么:(任何帮助都会很棒

【问题讨论】:

  • 你为什么要设置position=0
  • 这是我尝试将位置传回全局变量以查看是否可行

标签: java android image-gallery baseadapter


【解决方案1】:

改变这些:

 public int getCount() {
    return soundcloudDates.length;
}

public Object getItem(int position) {
    return soundcloudDates[position];
}

public View getView(int position, View convertView, ViewGroup parent) {
    // Not using convertView for simplicity. You should probably use it
    // in real application to get better performance.
    ImageView imageView = new ImageView(context);
    String text = (String) getItem( position );
    int resId;
    switch (position) {
    case 0:
        resId = R.drawable.logo;
            break;
    case 1:
        resId = R.drawable.soundcloudtest;
            break;
    case 2:
        resId = R.drawable.logo;
            break;
    case 3:
        resId = R.drawable.ic_launcher;
            break;
    default:
        resId = R.drawable.ic_launcher;
    }
    dateView.setText(text);
    imageView.setImageResource(resId);
    return imageView;
}

【讨论】:

  • 什么都没有改变,就像在 textview 改变之前一样,但它似乎是随机的。图像会将字段设置为图像 1,然后当我返回它时,它将是图像 4
  • 您的 dateView TextView 似乎没有在您的 getView() 方法中初始化。
  • 好的,我该如何初始化它?更新到您的最新建议不起作用。顺便谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2014-04-08
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-12
相关资源
最近更新 更多