以下是我的代码:
public class TestActivity extends ListActivity {
private Cursor cursor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
fillData();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
}
private void fillData() {
TermsData_DataHelper termsDataHelper = new TermsData_DataHelper(
TestActivity.this);
cursor = termsDataHelper.fetchAllCategories();
startManagingCursor(cursor);
String[] names = new String[] { "name" };
int[] to = new int[] { R.id.label };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter categories = new SimpleCursorAdapter(this,
R.layout.terms_row, cursor, names, to);
setListAdapter(categories);
}
}
当我单击 ListView 中的一行时,我没有看到任何 Toast。谁能告诉我哪里出错了。
提前致谢。
最佳答案
我遇到了类似的问题,发现从我的列表项中删除 android:clickable="true"
后,onListItemClick() 被触发得很好。我希望这会有所帮助。
关于Android:Listview 的 onItemClick() 事件未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6128219/