(Android)Listview onItemClicked 未触发

发布于 2024-11-07 19:16:10 字数 1286 浏览 0 评论 0原文

我正在开发一个杂货列表应用程序来学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2024-11-14 19:16:10

我的猜测是,您的列表行中有可聚焦的项目。我会考虑将 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"

婴鹅 2024-11-14 19:16:10

您可能想要使用 ListActivity,然后简单地重写 onListItemClick 方法。另一个问题可能是您在列表视图中放置的内容。有关概述,请参阅带有可点击/可编辑小部件的ListView,但如果您如果列表视图中有任何可聚焦的项目,您将很难使其正确注册点击。

You probably want to use a ListActivity and then simply override the onListItemClick 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文