【问题标题】:ListView Click eventListView 点击事件
【发布时间】:2009-11-10 05:45:52
【问题描述】:

我有一个从我的 Sqlite DB 中检索到的项目列表... 我想为每个项目设置一个 Click 事件。如何根据单击的项目自定义此事件???? 描述性强...我是初学者。

这是我用来在我的列表中填充数据的方法:

private void fillData() {
    db = new DBAdapter(this);
    db.open();
    ArrayList db_results = new ArrayList();
    //All Category
    //Cursor cursor = db.getAllTitles();

    //Single Category
    Cursor cursor = db.getTitle(1);
    if (cursor.moveToFirst())
    {
        do {          
            db_results.add(cursor.getString(4));
        } while (cursor.moveToNext());
    }
    cursor.close();

    this.list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, db_results));
}

【问题讨论】:

  • 当你点击不同的列表项时,你想要什么不同的行为?这个问题让我很困惑。

标签: android listview


【解决方案1】:

ListView 上致电setOnItemClickListener()。您提供的 AdapterView.OnItemClickListener 侦听器将获得位置(基于0 的索引)和 ID(如果您使用的是 CursorAdapter,那么您应该这样做,而不是将 Cursor 转换为 ArrayList) ,因此您将知道单击了哪个项目。

【讨论】:

  • 嗨,马克,我想做同样的事情。在网上我看到你所建议的就是这样做的方法,但我在任何地方都找不到像样的代码示例。你的书里有吗?如果是这样,请提供页码:)
  • 书中的许多 ListView 示例使用 ListActivity 并覆盖 onListItemClick(),它 (IIRC) 提供相同的参数。以github.com/commonsguy/cw-android/tree/master/Selection/List 为例。
  • 再次感谢马克,看起来正是我们需要的:)
猜你喜欢
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 2012-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多