(Android)Listview onItemClicked 未触发
我正在开发一个杂货列表应用程序来学习android,但我似乎无法弄清楚如何获取ListView中的项目来触发我在listview上设置的ItemClickListener中的onClick事件。 renderList() 方法是从 onResume() 方法中调用的,该方法从数据库中获取杂货列表并填充 ListView。为什么这个实现不起作用;当我单击 list_item 时,toast 不显示?
list_item 是一个复选框视图。
@Override
protected void onResume() {
super.onResume();
renderList();
}
protected void renderList(){
try {
dbHelper = new DbHelper(getApplicationContext());
db = dbHelper.getReadableDatabase();
cursor = db.query(DbHelper.TABLE, null, null, null, null, null, DbHelper.C_ID + " DESC");
startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, FROM, TO);
groceriesList = (ListView)findViewById(R.id.listView1);
groceriesList.setAdapter(adapter);
groceriesList.setOnItemClickListener(clickListen);
} catch (Exception e) {
Log.d(TAG, "RenderList Error: ",e);
}
}
private OnItemClickListener clickListen = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "pos: "+position, Toast.LENGTH_SHORT).show();
}
};
I am working on a groceries list app to learn android, but I cannot seem to figure out how to get the items in the ListView to trigger the onClick event in the ItemClickListener that I set on the listview. The method renderList() is called from the onResume() method, which grabs the list of groceries from the database and fills the ListView. Why does this implementation not work; the toast does not display when I click the list_item?
The list_item is a CheckBox view.
@Override
protected void onResume() {
super.onResume();
renderList();
}
protected void renderList(){
try {
dbHelper = new DbHelper(getApplicationContext());
db = dbHelper.getReadableDatabase();
cursor = db.query(DbHelper.TABLE, null, null, null, null, null, DbHelper.C_ID + " DESC");
startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, FROM, TO);
groceriesList = (ListView)findViewById(R.id.listView1);
groceriesList.setAdapter(adapter);
groceriesList.setOnItemClickListener(clickListen);
} catch (Exception e) {
Log.d(TAG, "RenderList Error: ",e);
}
}
private OnItemClickListener clickListen = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "pos: "+position, Toast.LENGTH_SHORT).show();
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,您的列表行中有可聚焦的项目。我会考虑将 this 设置为 false 或在项目绑定中,只需将其 xml 属性设置为 android:focusable="false"
My guess is that you have items in your list rows that are focusable. I would look into setting this to false or in your binding of items, just set their xml attributes to android:focusable="false"
您可能想要使用
ListActivity
,然后简单地重写onListItemClick
方法。另一个问题可能是您在列表视图中放置的内容。有关概述,请参阅带有可点击/可编辑小部件的ListView,但如果您如果列表视图中有任何可聚焦的项目,您将很难使其正确注册点击。You probably want to use a
ListActivity
and then simply override theonListItemClick
method. The other issue is probably what you are putting in the list view. See ListView with clickable/editable widget for an overview, but if you have any focusable items in the list view you'll have difficulties getting it to register clicks correctly.