每当点击发生在listview Android上时,从列表视图中挑选图像并放入imageview

时间:2011-11-24 09:08:14

标签: android android-layout

我有一个listview,其中包含activity-A中的图像和名称。 当我点击列表视图时,它必须移动到另一个活动-B。

在活动B中,有一个imageButton。 当我点击活动-A中列表视图中的任何项目时,与该被点击项目对应的图像必须显示在activity-B的imageButton

我搜索了更多但没有得到正确答案。

请建议我解决这个问题的麻烦答案

提前致谢...

2 个答案:

答案 0 :(得分:2)

我希望下面的代码对你有用。

第一次活动

ListView listView;
int total_data = 11;

// references to our images
private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
        R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
        R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
        R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7 };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(new ImageAdapter());

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    mThumbIds[arg2]);

            Intent intent = new Intent(StackTestActivity.this,
                    StackTestActivity1.class);
            intent.putExtra("bitmap", bitmap);

            startActivity(intent);

        }
    });
}

private class ImageAdapter extends BaseAdapter {

    public ImageAdapter() {

    }

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

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

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

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

        View v;
        if (convertView == null) {
            LayoutInflater li = getLayoutInflater();

            v = li.inflate(R.layout.customview, null);

        } else {
            v = convertView;
        }

        TextView text = (TextView) v.findViewById(R.id.textView1);
        text.setText("Image Text");

        ImageView icon = (ImageView) v.findViewById(R.id.imageView1);
        icon.setImageResource(mThumbIds[position]);

        return v;
    }

}

第二次活动

ImageView button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    button=(ImageView)findViewById(R.id.imageView1);

    Bundle extra=getIntent().getExtras();

    if(extra!=null)
    {
        Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("bitmap");
        button.setImageBitmap(bitmap);
    }

}

答案 1 :(得分:0)

您可以在此处查看有关此问题的一些答案:Get/pick an image from Android's built-in Gallery app programmatically

通常,假设您已将图像保存在某个位置,您可以发送意图以通过库查看它,也可以打开文件,读取它,然后将其设置为ImageView。

希望这有帮助!