如何处理列表视图中点击事件的android? [英] How to handle the click event in Listview in android?

查看:264
本文介绍了如何处理列表视图中点击事件的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的测试code创建列表视图,列表视图中显示成功,但是,没有在点击事件的错误。我想创建一个意图发送硬code消息到一个新的活动。然而,它显示错误行

 意向意图=新的意图(背景下,SendMessage.class);
 

所以,问题是,我应该为这个类呢?

另外,代替c中的输出消息硬$ C $,如何捕捉在列表视图中的行的数据,并传递到新的活动?例如 BBB,AAA,R.drawable.tab1_hdpi 的第一行。

感谢。

 公共类MainActivity延伸活动{
    公共最后静态字符串EXTRA_MESSAGE =com.example.ListViewTest.MESSAGE;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        ArrayList的<一个ListEntry>成员=新的ArrayList<一个ListEntry>();
        members.add(新一个ListEntry(BBB,AAA,R.drawable.tab1_hdpi));
        members.add(新一个ListEntry(CCC,DDD,R.drawable.tab2_hdpi));
        members.add(新一个ListEntry(阿萨,第一百一十五号,R.drawable.tab3_hdpi));
        members.add(新一个ListEntry(BcxsadvBB,AcxdxvAA));
        members.add(新一个ListEntry(BcxvadsBB,AcxzvAA));
        members.add(新一个ListEntry(BcxvBB,AcxvAA));
        members.add(新一个ListEntry(BvBB,AcxsvAA));
        members.add(新一个ListEntry(BcxvBB,AcxsvzAA));
        members.add(新一个ListEntry(Bcxadv,AcsxvAA));
        members.add(新一个ListEntry(BcxcxB,AcxsvAA));
        ListView的LV =(ListView控件)findViewById(R.id.listView1);
        Log.i(testTag,启动适配器之前);
        StringArrayAdapter广告=新StringArrayAdapter(成员,这一点);
        Log.i(testTag,启动适配器之后);
        Log.i(testTag,设置适配器);
        lv.setAdapter(广告);
        lv.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
                    长ID){
                意向意图=新的意图(背景下,SendMessage.class);
                字符串消息=ABC;
                intent.putExtra(EXTRA_MESSAGE,消息);
                startActivity(意向);
            }
        });
    }
 

解决方案

我看不到你在哪里申报上下文。在意向创作,你可以使用的目的 MainActivity.this

  lv.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
                    长ID){
                意向意图=新的意图(MainActivity.this,SendMessage.class);
                字符串消息=ABC;
                intent.putExtra(EXTRA_MESSAGE,消息);
                startActivity(意向);
            }
        });
 

要检索的对象后,你点击就可以使用适配器视图

 一个ListEntry项=(一个ListEntry)parent.getItemAtPosition(位置);
 

The below is my testing code to create the list view, the list view display successfully, however, there is error in click event. I would like to create an intent to send a hardcode message to an new activity. However, it show error for the line

Intent intent = new Intent(context, SendMessage.class);

So , the problem is , what should I provide for this class?

Also , instead of hard code the output message, how to capture the data in list view row and pass to the new activity? e.g. BBB,AAA,R.drawable.tab1_hdpi for the first row.

Thanks.

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<ListEntry> members = new ArrayList<ListEntry>(); 
        members.add(new ListEntry("BBB","AAA",R.drawable.tab1_hdpi));
        members.add(new ListEntry("ccc","ddd",R.drawable.tab2_hdpi));
        members.add(new ListEntry("assa","cxv",R.drawable.tab3_hdpi));
        members.add(new ListEntry("BcxsadvBB","AcxdxvAA"));
        members.add(new ListEntry("BcxvadsBB","AcxzvAA"));
        members.add(new ListEntry("BcxvBB","AcxvAA"));
        members.add(new ListEntry("BvBB","AcxsvAA"));
        members.add(new ListEntry("BcxvBB","AcxsvzAA"));
        members.add(new ListEntry("Bcxadv","AcsxvAA"));
        members.add(new ListEntry("BcxcxB","AcxsvAA"));
        ListView lv = (ListView)findViewById(R.id.listView1);
        Log.i("testTag","before start adapter");
        StringArrayAdapter ad = new StringArrayAdapter (members,this);
        Log.i("testTag","after start adapter");
        Log.i("testTag","set adapter");
        lv.setAdapter(ad);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                Intent intent = new Intent(context, SendMessage.class);
                String message = "abc";
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);
            }
        });
    }

解决方案

I can not see where do you declare context. For the purpose of the intent creation you can use MainActivity.this

 lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                Intent intent = new Intent(MainActivity.this, SendMessage.class);
                String message = "abc";
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);
            }
        });

To retrieve the object upon you have clicked you can use the AdapterView:

ListEntry entry = (ListEntry) parent.getItemAtPosition(position);

这篇关于如何处理列表视图中点击事件的android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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