安卓:Listview的onItemClick()事件没有被调用

4
以下是我的代码:
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);
    }
}

当我点击列表视图中的一行时,我没有看到任何Toast。有人能告诉我我犯了什么错误吗?
提前感谢。

1
可能会有其他人很快解决这个问题,但是我想提一下:在onListItemClick()方法中设置断点并在调试模式下运行,可以确保该方法是否被调用,这可能会缩小可能出现问题的范围。 - Sven Viking
@Viking:我在onListItemClick()处添加了一个断点,但当我点击列表行时,控制并没有到达那里。你能告诉我可能的错误是什么吗? - Mahendra Liya
我在这里找到了解决此问题的方法:https://dev59.com/3m035IYBdhLWcg3wNtRw - Oded Regev
4个回答

4
我遇到了类似的问题,并发现从我的列表项中删除android:clickable="true"后,onListItemClick()就正常触发了。希望这可以帮到你。

2
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    String mString = parent.getItemAtPosition(position).toString();
    //will give you the text of current line or
    int i = parent.getItemAtPosition(position);
    //if you want just position number
}


1
我也遇到了这个问题。我通过在布局XML中从我的TextView中删除这个来解决它。
    android:textIsSelectable="true"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接