如何在Android的ListView中处理点击事件?
以下是我的testing代码创build列表视图,列表视图显示成功,但是,在点击事件中有错误。 我想创build一个意图发送硬编码信息到一个新的活动。 但是,它显示错误的行
Intent intent = new Intent(context, SendMessage.class);
所以,问题是,我应该为这个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); } }); }
我看不出你在哪里声明context
。 为了创build目的,您可以使用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); } });
点击后检索对象,可以使用AdapterView
:
ListEntry entry = (ListEntry) parent.getItemAtPosition(position);
ListView具有项目点击侦听器callback。 你应该在ListView
设置onItemClickListener
。 callback包含AdapterView
和position
作为参数。 哪个可以给你ListEntry
。
lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListEntry entry= (ListEntry) parent.getAdapter().getItem(position); Intent intent = new Intent(MainActivity.this, SendMessage.class); String message = entry.getMessage(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } });
正如你所说,错误来自于你的代码
Intent intent = new Intent(context, SendMessage.class);
这是由于您将OnItemClickListener匿名类的上下文提供给Intent构造函数,但根据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() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListEntry entry= (ListEntry) parent.getAdapter().getItem(position); Intent intent = new Intent(MainActivity.this, SendMessage.class); intent.putExtra(EXTRA_MESSAGE, entry.getMessage()); startActivity(intent); } });
请让我知道这是否有助于你
编辑: –如果你正在发现一些问题,以获得列表的价值做一件事情declear你的数组列表
ArrayList<ListEntry> members = new ArrayList<ListEntry>();
全局即在创build和更改您的监听器之前
lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, SendMessage.class); intent.putExtra(EXTRA_MESSAGE, members.get(position)); startActivity(intent); } });
所以你的整个代码看起来像
public class MainActivity extends Activity { public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE"; ArrayList<ListEntry> members = new ArrayList<ListEntry>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 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(MainActivity.this, SendMessage.class); intent.putExtra(EXTRA_MESSAGE, members.get(position).getMessage()); startActivity(intent); } }); }
其中getMessage()将是您的ListEntry类中指定的getter方法,您正在使用它来获取之前设置的消息。
首先,类必须实现点击监听器:
implements OnItemClickListener
然后设置一个监听器到ListView
yourList.setOnItemclickListener(this);
最后,创buildclic方法:
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "You Clicked at ", Toast.LENGTH_SHORT).show(); }
//get main activity final Activity main_activity=getActivity(); //list view click listener final ListView listView = (ListView) inflatedView.findViewById(R.id.listView_id); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String stringText; //in normal case stringText= ((TextView)view).getText().toString(); //in case if listview has separate item layout TextView textview=(TextView)view.findViewById(R.id.textview_id_of_listview_Item); stringText=textview.getText().toString(); //show selected Toast.makeText(main_activity, stringText, Toast.LENGTH_LONG).show(); } }); //populate listview
根据我的testing,
-
实现OnItemClickListener – >工程。
-
setOnItemClickListener – >工作。
-
ListView默认是可点击的(API 19)
重要的是,“点击”仅适用于TextView(如果selectsimple_list_item_1.xml作为项目)。 这意味着,如果您为ListView提供文本数据,则单击文本区域时“单击”将起作用。 点击空白区域不会触发“点击事件”。