1

我是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);

}

}

4

3 回答 3

3

而不是 setOnClickListener() 为您的行,尝试使用 setOnItemClickListener() 为您的 ListView:

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);

            }

        });
于 2012-09-29T08:21:36.637 回答
0

在动态分配 Resource 给您的Imageview.

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

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

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

希望这可以帮助。

于 2012-09-29T08:42:03.140 回答
0

只需在图像数组上发送图像的位置并在第二个活动中检索就足够了......在第一个活动中:

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

在第二个活动中:

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

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

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

于 2012-09-29T09:44:34.917 回答