如何将 Listview 选定的项目值传递给另一个活动 [英] How to pass Listview selected item values to another activity

查看:19
本文介绍了如何将 Listview 选定的项目值传递给另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have successfully added Images from a ListView to a detailed View, when an item in the list is clicked on.But how would I add longer descriptions (which would not be shown in the list, but only appear in a textView in the single_list_item xml?Here's my current code for sending images(string "web" is short text descriptions, string ImageId is the list of images) to my 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 中的代码:

And here's my current code in 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 的字符串发送较长的文本?

Could anyone show how I send over the longer text from, say, a string called longdesc?

推荐答案

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

在 singlelistitem 类中

in the singlelistitem class

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

这篇关于如何将 Listview 选定的项目值传递给另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆