如何在Android中的Listview中处理click事件?

以下是我创建列表视图的测试代码,列表视图成功显示,但是单击事件中有错误。我想创建一个向新活动发送硬编码消息的意图。但是,它显示该行的错误


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

那么,问题是,我应该为这堂课提供什么?


另外,如何代替硬编码输出消息,如何捕获列表视图行中的数据并将其传递给新活动?例如BBB,AAA,R.drawable.tab1_hdpi第一行。


谢谢。


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

            }

        });

    }


温温酱
浏览 467回答 3
3回答

幕布斯7119047

我看不到你在哪里申报context。出于意图创建的目的,您可以使用MainActivity.this&nbsp;lv.setOnItemClickListener(new OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(MainActivity.this, SendMessage.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String message = "abc";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(EXTRA_MESSAGE, message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });单击后要检索对象,可以使用AdapterView:ListEntry entry = (ListEntry) parent.getItemAtPosition(position);

叮当猫咪

ListView具有Item单击侦听器回调。您应该在onItemClickListener中设置ListView。回调包含AdapterView和position作为参数。哪个可以给你ListEntry。lv.setOnItemClickListener(new OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListEntry entry= (ListEntry) parent.getAdapter().getItem(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(MainActivity.this, SendMessage.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String message = entry.getMessage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(EXTRA_MESSAGE, message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });

九州编程

如您所说,此语句中的代码中出现错误Intent intent = new Intent(context, SendMessage.class);这是由于您向Intent构造函数中提供了OnItemClickListener匿名类的上下文,但根据Intent的构造函数android.content.Intent.Intent(Context packageContext, Class<?> cls)您必须提供您使用Intent的活动上下文,即MainActivity类上下文。因此,出现错误的语句将被转换为Intent intent = new Intent(MainActivity.this, SendMessage.class);另外,要将您的消息从此MainActivity发送到SendMessage类,请参见以下代码lv.setOnItemClickListener(new OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListEntry entry= (ListEntry) parent.getAdapter().getItem(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(MainActivity.this, SendMessage.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(EXTRA_MESSAGE, entry.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });请告诉我这是否对您有帮助编辑:- 如果您发现某些问题以获得列表的值,请执行一件事清除您的数组列表ArrayList<ListEntry> members = new ArrayList<ListEntry>();全局,即在oncreate和更改您的监听器之前,如下所示&nbsp;lv.setOnItemClickListener(new OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(MainActivity.this, SendMessage.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(EXTRA_MESSAGE, members.get(position));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });所以你的整个代码看起来像public class MainActivity extends Activity {&nbsp; &nbsp; public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE";ArrayList<ListEntry> members = new ArrayList<ListEntry>();&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("BBB","AAA",R.drawable.tab1_hdpi));&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("ccc","ddd",R.drawable.tab2_hdpi));&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("assa","cxv",R.drawable.tab3_hdpi));&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("BcxsadvBB","AcxdxvAA"));&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("BcxvadsBB","AcxzvAA"));&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("BcxvBB","AcxvAA"));&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("BvBB","AcxsvAA"));&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("BcxvBB","AcxsvzAA"));&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("Bcxadv","AcsxvAA"));&nbsp; &nbsp; &nbsp; &nbsp; members.add(new ListEntry("BcxcxB","AcxsvAA"));&nbsp; &nbsp; &nbsp; &nbsp; ListView lv = (ListView)findViewById(R.id.listView1);&nbsp; &nbsp; &nbsp; &nbsp; Log.i("testTag","before start adapter");&nbsp; &nbsp; &nbsp; &nbsp; StringArrayAdapter ad = new StringArrayAdapter (members,this);&nbsp; &nbsp; &nbsp; &nbsp; Log.i("testTag","after start adapter");&nbsp; &nbsp; &nbsp; &nbsp; Log.i("testTag","set adapter");&nbsp; &nbsp; &nbsp; &nbsp; lv.setAdapter(ad);&nbsp; &nbsp; &nbsp; &nbsp; lv.setOnItemClickListener(new OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(MainActivity.this, SendMessage.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(EXTRA_MESSAGE, members.get(position).getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }其中getMessage()是在ListEntry类中指定的getter方法,用于获取先前设置的消息。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android