使用 Intent 开始新详细信息 Activity 时传递的内容错误

Passed Content Wrong When Starting New Detail Activity with Intent

我正在通过 ListViewItem 的意图开始一个新的 Activity。但是,新的详细信息 Activity 在 -1 位置显示 ListViewItem 的信息。

我在此处实例化并启动 Activity:

ListView list = (ListView)findViewById(R.id.merchListView);
list.setAdapter(merchItemArrayAdapter);

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Toast.makeText(getApplication(),String.format("view==>%s, position ==> %s, id==> %s",view.toString(),position,id),Toast.LENGTH_SHORT).show();
        System.out.println(String.format("view==>%s, position ==> %s, id==> %s",view.toString(),position,id));

        //Build Intent
        Intent intent = new Intent(getApplicationContext(),MerchItemDescription.class);


        TextView tv1 = (TextView) findViewById(R.id.textView2);
        TextView tv2 = (TextView) findViewById(R.id.costView);
        ImageView iv1 = (ImageView) findViewById(R.id.imageView);


        System.out.println(String.format("Imageview id is ==> %d",R.id.imageView));

        iv1.buildDrawingCache();
        Bitmap imageViewImage = iv1.getDrawingCache();

        Bundle extras = new Bundle();
        extras.putParcelable("imagebitmap", imageViewImage);
        //extras.putParcelable("theView", view);
        intent.putExtras(extras);

        String message = tv1.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);


        String costViewString = tv2.getText().toString();
        intent.putExtra(ITEM_COST,costViewString);





        startActivity(intent);

    }
});

这是传递给 Intent 的 class:

setContentView(R.layout.merch_item_description);

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivityTest.EXTRA_MESSAGE);


        System.out.println(String.format("Message ==> %s", message));

        //View merchDescView = getLayoutInflater().inflate(R.layout.merch_item_description, null, false);


        Bundle extras = getIntent().getExtras();
        Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
        //View myView = (View) extras.getParcelable("theView");

        TextView txtView2 = (TextView)findViewById(R.id.textViewLongDesc);

        //TextView txtView2 = (TextView) myView.findViewById(R.id.textViewLongDesc);

        txtView2.setText(message);


        ImageView iv = (ImageView)findViewById(R.id.itemDescIV);

        //ImageView iv = (ImageView) myView.findViewById(R.id.itemDescIV);


        iv.setImageBitmap(bmp);

如果 tv1、tv2 和 iv1 是 ListView 中项目的子项,而不是:

TextView tv1 = (TextView) findViewById(R.id.textView2);
TextView tv2 = (TextView) findViewById(R.id.costView);
ImageView iv1 = (ImageView) findViewById(R.id.imageView);

你的意思可能是:

TextView tv1 = (TextView) view.findViewById(R.id.textView2);
TextView tv2 = (TextView) view.findViewById(R.id.costView);
ImageView iv1 = (ImageView) view.findViewById(R.id.imageView);

它可能会在 Activity 而不是子视图中找到第一个 R.id.*。