0

如何在 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);  
}
4

2 回答 2

2

在您的onActivityCreated使用中

ListView lv = getListView();
lv.setOnClcikListener();

如果ListActivity,ListFragment其他明智的做法getListView()行不通,那么您必须findViewById这样做

于 2012-04-29T05:38:18.783 回答
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;
}
于 2012-04-29T06:06:06.087 回答