【问题标题】:Get selected item from ListView bound with SimpleCursorAdapter从与 SimpleCursorAdapter 绑定的 ListView 中获取所选项目
【发布时间】:2011-05-27 19:29:02
【问题描述】:

我是 Android 开发的新手...来自 iPhone 和 .Net 背景。我见过与这个问题非常相似的问题,但没有一个涉及 SimpleCursorAdapter。

我有一个基本的 ListActivity,它使用 Cursor 将 SQLite 查询中的数据绑定到我的 ListView:

ListAdapter adapter = new SimpleCursorAdapter(
        this, 
        android.R.layout.simple_list_item_1,  
        c,        
        new String[] {"name"},   
        new int[] {android.R.id.text1}); 

setListAdapter(adapter);

然后当一个项目被点击时:

public void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position,  id);

    //Difference between this:
    Cursor c = (cursor)l.getItemAtPosition(position);
    //and this??
    Cursor c = (Cursor)l.getAdapter().getItem(position);

    int categoryId = c.getInt(0);
}

这是获取所选元素 id 的正确方法吗?这似乎很奇怪,因为我认为在数据库关闭后(即在我绑定之后)我不能使用我的光标。当我似乎无法找到从该 id 获取实际项目的方法时,传入的 id 有什么意义?另外,我不明白为什么 getItemAtPosition() 会返回一个游标......游标绑定到整个列表;不只是一排。最后,如果这是正确的,那么显示的两种获取光标的方式有区别吗?谢谢。

【问题讨论】:

    标签: android listview simplecursoradapter


    【解决方案1】:

    所以有几点:获取光标后,您想调用startManagingCursor。这将游标的生命周期与 Activity 的生命周期联系起来(因此当 Activity 被销毁时,游标会被关闭/清理)。

    startManagingCursor(c);
    ListAdapter adapter = new SimpleCursorAdapter(
            this, 
            android.R.layout.simple_list_item_1,  
            c,        
            new String[] {"name"},   
            new int[] {android.R.id.text1}); 
    setListAdapter(adapter);
    

    此外,数据库没有关闭,光标通常保持与数据库的实时连接(因此 ListView 可以滚动并执行可能需要将来访问光标内容的那种性质的事情.

    对于您的核心问题,onListItemClick 中最简单的方法是:

    Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
    c.moveToPosition(position);
    

    然后您可以使用c.getLong(0) 来获取 id(假设您将 id 列作为第一列获取,这通常是这种情况)。但是,请注意,id 是作为签名的一部分传入的(请参阅public void onListItemClick(ListView l, View v, int position, long id) 中的最后一个参数),因此您确实不需要再次获取它(但如果您想烧掉循环,您当然可以)。要访问其他列,您可以执行相同的操作,只需更改列索引。

    希望对您有所帮助。

    【讨论】:

    • 谢谢!我正在调用 startManagingCursor,但直到现在我才明白为什么!几个问题......所以即使我调用 SQLiteDatabase.close(),这并不能阻止我的光标访问它?而且 ListView 似乎没有 getCursor() 方法。最后,传递给我的方法的 id 是自动来自我的数据库的 id 吗?谢谢!
    • 啊,如果您调用 close,那么您的 ListView 可能会停止运行:通常我所做的是将我的 Application 对象重用为我的数据库适配器:在 onCreate 中调用 open 并在 @987654329 中关闭@ 然后使用它将数据库连接生命周期与应用程序的生命周期联系起来。至于getCursor,我的错误:应该是Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();。是的,id 是从数据库中出来的:它非常方便。
    • startManagingCursor 在 APi11 中已弃用,您可能需要更新您的答案以使用 CursorLoader 提及
    • startManagingCursor 仅在您的目标是 0.0001% 拥有 HoneyComb 或 ICS 或想要处理版本化导入疯狂的 Android 用户之一时被弃用。
    • @BjornTipling 实际上你可以使用带有 Compatibility 包的 CursorLoaders,更新这么棒的答案有什么问题。
    【解决方案2】:

    另一种方式:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
    
                Cursor cursor = (Cursor) parent.getAdapter().getItem(position);
                //TODO
                }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多