如何通过列表视图中选择项值到另一个活动 [英] How to pass Listview selected item values to another activity

查看:113
本文介绍了如何通过列表视图中选择项值到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功添加图像从一个ListView仔细查看,当在列表中的项目被点击on.But我怎么想补充更详细的描述(这将不会在列表中显示,但只出现在一个TextView在single_list_item XML?这是我目前的code发送图像(字符串网是简短的文字描述,串ImageId是图像列表)我SingleListItem.java。

 的setContentView(R.layout.hotels);CustomList适配器=新CustomList(Hotels.this,网络,imageId);
名单=(ListView控件)findViewById(R.id.list);
list.setAdapter(适配器);
list.setOnItemClickListener(新AdapterView.OnItemClickListener(){
    @覆盖
    公共无效onItemClick(适配器视图<>母公司,观景,
                            INT位置,长的id){
        ImageView的imgview =(ImageView的)view.findViewById(R.id.img);
        // INT图像= imgview.getId();
        INT图像=位置;
        // INT图像= parent.getAdapter()的getItem(位置)。
        //Toast.makeText(MainActivity.this,+网站[+位置],Toast.LENGTH_SHORT).show()你在点击了;
        意图I =新意图(getApplicationContext(),SingleListItem.class);
        //i.putExtra(\"zurag,图像);
        i.putExtra(zurag,imageId [位置]);
        startActivity(ⅰ);
    }
});

这是我目前的code在SingleListItem.java:

 公共类SingleListItem延伸活动{
    Button按钮;
    按钮按钮2;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_item_view);        ImageView的图像=(ImageView的)findViewById(R.id.imageView1);
        意向I = getIntent();
        INT PIC = i.getIntExtra(zurag,0);
        images.setImageResource(PIC);
        TextView的文本=(的TextView)findViewById(R.id.textView1);
        意向I2 = getIntent();
        INT文本1 = i2.getIntExtra(zurag,0);
        texts.setText(文本1);        addListenerOnButton();
    }
}

任何人都可以表明我怎么送过来了较长的文本,例如从一个名为字符串 LONGDESC


解决方案

 意向书I =新意图(getApplicationContext(),SingleListItem.class);
            i.putExtra(海峡,LONGDESC);
            i.putExtra(zurag,imageId [位置]);
            startActivity(ⅰ);

在singlelistitem类

 意向书I = getIntent();
        意向I2 = getIntent();
        字符串的text1 = i2.getStringExtra(STR);
        texts.setText(文本1);

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

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

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

in the singlelistitem class

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

这篇关于如何通过列表视图中选择项值到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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