如何将Listview选定的项值传递给另一个活动

时间:2015-09-04 12:56:38

标签: android android-listview

当单击列表中的项目时,我已成功将ListView中的图像添加到详细视图中。但是我如何添加更长的描述(这些描述不会显示在列表中,而只会显示在textView中single_list_item xml?这是我当前用于发送图像的代码(字符串" web"是短文本描述,字符串ImageId是图像列表)到我的SingleListItem.java。

setContentView(R.layout.hotels);

CustomList adapter = new CustomList(Hotels.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        ImageView imgview = (ImageView) view.findViewById(R.id.img);
        //int images = imgview.getId();
        int images = position;
        //int images = parent.getAdapter().getItem(position);
        //Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
        Intent i = new Intent(getApplicationContext(), SingleListItem.class);
        //i.putExtra("zurag", images);
        i.putExtra("zurag", imageId[position]);
        startActivity(i);
    }
});

这是我在SingleListItem.java中的当前代码:

public class SingleListItem extends Activity{
    Button button;
    Button button2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_item_view);

        ImageView images = (ImageView) findViewById(R.id.imageView1);
        Intent i = getIntent();
        int pic =i.getIntExtra("zurag", 0);
        images.setImageResource(pic);
        TextView texts = (TextView) findViewById(R.id.textView1);
        Intent i2 = getIntent();
        int text1 =i2.getIntExtra("zurag", 0);
        texts.setText(text1);

        addListenerOnButton();
    }
}

有人能说明我如何通过名为longdesc的字符串发送更长的文字吗?

2 个答案:

答案 0 :(得分:4)

    Intent i = new Intent(getApplicationContext(), SingleListItem.class);
            i.putExtra("str", longdesc);
            i.putExtra("zurag", imageId[position]);
            startActivity(i);
在singlelistitem类

 Intent i = getIntent();
        Intent i2 = getIntent();
        String text1 =i2.getStringExtra("str");
        texts.setText(text1);

答案 1 :(得分:1)

发送字符串:

i.putExtra("longdesc", longdesc);

在目的地读取字符串:

String longdesc = i.getStringExtra("longdesc");