Android BaseAdapter,返回商品ID

时间:2013-12-12 10:54:48

标签: java android image-gallery baseadapter

我试图得到它,以便在显示图像1时,textview显示其名称

这是我的名字数组

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

这是我使用GitHub的EcoGallery库的BaseAdapter 所有图像都正确滚动,但他们的id输出到处都是 通常它会开始打印出position = 1;然后在position = 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()放在不同的地方无效。

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

1 个答案:

答案 0 :(得分: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;
}