如何列出视图图像传递另一个活动

时间:2012-09-29 08:01:47

标签: android image listview android-intent android-activity

我是android开发的初学者。 这是我的列表图像代码。 它不会传递图像另一个活动。为什么它不传递图像另一个活动。任何人都可以解决它?

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

        LayoutInflater inflater = getLayoutInflater();

        View row = inflater.inflate(R.layout.row, parent, false);

        TextView textview = (TextView) row.findViewById(R.id.tvTop);
        ImageView imageview = (ImageView) row.findViewById(R.id.list_image);

        textview.setText(data_topText[position]);
        imageview.setImageResource(data_image[position]);

        row.setOnClickListener(new OnClickListener() {

            public void onClick(View row) {
                    TextView textview = (TextView) row.findViewById(R.id.tvTop);
                    ImageView imageview = (ImageView) row.findViewById(R.id.list_image);


                    String product = textview.getText().toString();
                    int images = imageview.getId();


                    Intent i = new Intent(getApplicationContext(),
                            SecondScreenActivity.class);

                    i.putExtra("name", product);
                    i.putExtra("zurag", images);
                    startActivity(i);
            }
        });

        return (row);

    }

这是SecondScreenActivity.java:

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

    TextView txtName = (TextView) findViewById(R.id.txtName);
    ImageView images = (ImageView) findViewById(R.id.image);

    Intent intent = getIntent();

    String name = intent.getStringExtra("name");
    int pic =intent.getIntExtra("zurag", 0);

    txtName.setText(name);
    images.setImageResource(pic);

}

}

3 个答案:

答案 0 :(得分:3)

代替您的行的setOnClickListener(),尝试对ListView使用setOnItemClickListener():

lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View row, int position, long id) {
                TextView textview = (TextView)row.findViewById(R.id.tvTop);
                    ImageView imageview = (ImageView) row.findViewById(R.id.list_image);


                    String product = textview.getText().toString();
                    int images = imageview.getId();


                    Intent i = new Intent(getApplicationContext(),
                            SecondScreenActivity.class);

                    i.putExtra("name", product);
                    i.putExtra("zurag", images);
                    startActivity(i);

            }

        });

答案 1 :(得分:0)

在动态为您的Imageview分配资源时尝试使用setTag()和getTag()。

int images = getID(); // Will Not Return you Image Resource you set.

当分配ImageResource尝试设置标记标识时,这将帮助您在第二个活动中检索图像资源。

imageView.setTag("any tag");
String imageTag =  (String) imageView.getTag(); // To retrieve

希望这有助于。

答案 2 :(得分:0)

足以在图像阵列上发送图像的位置并在第二个活动中检索... 在第1次活动中:

 i.putExtra("name", product);
 i.putExtra("pos", position);

在第二项活动中:

int pic =intent.getIntExtra("pos", 0);

//并设置    images.setImageResource(data_image [图]);

重要提示:即使在第二个活动中你也需要保留图像数组(即data_image)..或者你可以尝试发送位图......