如何setImageResource List <item>

时间:2017-02-17 09:21:24

标签: java android list gridview baseadapter

我有一个GridView。当我单击GridView项时,Page转到带有GridView Position的FullScreenActivity。在此Activity中,我使用GridView Activity Intent中的“id”。然后用位置setImageResource。我无法在List Item Object中设置setImageResource。我怎么解决这个问题?

public class MyAdapter extends BaseAdapter {
    List<Item> mItems = new ArrayList<Item>();
    LayoutInflater mInflater;

    public MyAdapter(Context context) {
        mInflater = LayoutInflater.from(context);    
        mItems.add(new Item("Red",       R.drawable.red));
        mItems.add(new Item("Magenta",   R.drawable.magenta));
        mItems.add(new Item("Dark Gray", R.drawable.dark_gray));
        mItems.add(new Item("Gray",      R.drawable.gray));
        mItems.add(new Item("Green",     R.drawable.green));
        mItems.add(new Item("Cyan",      R.drawable.cyan));
    }
    .........
}

GridViewActivity.java

GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
        i.putExtra("id", position);
        startActivity(i);
    }
});

FullScreenActivity.java

   Intent i = getIntent();
   int position = i.getExtras().getInt("id");
   MyAdapter imageAdapter = new MyAdapter(this);
   ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
   imageView.setImageResource(imageAdapter.mThumbIds[position]); // error

1 个答案:

答案 0 :(得分:0)

因为imageAdapter是新的。它不包括数据。

你应该发送图像资源。

GridViewActivity.java

GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Item item = parent.getAdapter().getItem(position);

    Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
    i.putExtra("imageResource", item.getImageResource);
    startActivity(i);
 }
});

FullScreenActivity.java

Intent i = getIntent();
int imageResource= i.getExtras().getInt("imageResource");
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageResource);
希望能帮到你。