如何在Listview wordList上设置onclickListener来检索列表中显示的文本?我还应该在哪里写这个功能?我是一个天真的Android开发人员,找不到合适的例子!
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
// Populate the wordsList with the String values the recognition engine thought it heard
matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
答案 0 :(得分:2)
在onActivityCreated
使用
ListView lv = getListView();
lv.setOnClcikListener();
如果ListActivity,ListFragment
其他明智的getListView()
无效,您必须findViewById
并执行相同的操作
答案 1 :(得分:2)
对于项目点击,请使用:
listView = (ListView) findViewById(R.id.list_view);
// Set adapter here
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
如果你想添加一个上下文菜单:
listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(0, 1, 0, "View");
menu.add(0, 2, 0, "Edit");
menu.add(0, 3, 0, "Delete");
}
});
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo menuInfo;
switch (item.getItemId()) {
case 1:
// Do something
break;
case 2:
// Do something
break;
case 3:
// Do something
default:
return super.onContextItemSelected(item);
}
return true;
}