简体   繁体   English

从与 SimpleCursorAdapter 绑定的 ListView 中获取所选项目

[英]Get selected item from ListView bound with SimpleCursorAdapter

I'm brand new to Android development... coming from iPhone and.Net background.我是 Android 开发的新手……来自 iPhone 和.Net 背景。 I've seen very similar questions to this one, but none of them dealt with the SimpleCursorAdapter.我见过与这个问题非常相似的问题,但没有一个涉及 SimpleCursorAdapter。

I have a basic ListActivity which uses a Cursor to bind data from a SQLite query to my ListView:我有一个基本的 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);

Then when an item is clicked:然后当一个项目被点击时:

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);
}

Is this the proper way to get the id of the element that was selected?这是获取所选元素的 id 的正确方法吗? It seems strange, because I wouldn't think I could use my cursor after the database is closed (which is is after I bind).这似乎很奇怪,因为我认为在数据库关闭后(在我绑定之后)我不能使用我的 cursor。 What is the point of the id passed in, when I can't seem to find a way of getting the actual item from that id?当我似乎无法找到从该 id 获取实际项目的方法时,传入的 id 有什么意义? Also, I don't understand why the getItemAtPosition() would return a cursor... the cursor is bound to the entire list;另外,我不明白为什么 getItemAtPosition() 会返回 cursor ... cursor 绑定到整个列表; not just one row.不只是一排。 Finally, if this is correct, is there a difference between the two ways shown of getting the cursor?最后,如果这是正确的,那么显示的两种获取 cursor 的方式之间是否有区别? Thanks.谢谢。

So a couple of points: after you fetch the cursor, you want to call startManagingCursor .所以有几点:在你获取 cursor 之后,你想调用startManagingCursor This ties the cursor's lifecycle with Activity's lifecycle (so when the Activity gets destroyed the cursor gets closed/cleaned up).这将光标的生命周期与 Activity 的生命周期联系起来(因此,当 Activity 被销毁时,cursor 将被关闭/清理)。

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);

Also, the database isn't closed , the Cursor typically keeps a live connection to the DB (so the ListView can scroll and do things of that nature that may require future access to the Cursor's contents.此外,数据库没有关闭,Cursor 通常保持与数据库的实时连接(因此 ListView 可以滚动并执行可能需要将来访问光标内容的那种性质的事情。

To your core question, the easiest way to do it in onListItemClick is this:对于您的核心问题,在onListItemClick中执行此操作的最简单方法是:

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

You can then use the c.getLong(0) to get the id (assuming you fetched the id column as the first column which is generally the case).然后,您可以使用c.getLong(0)来获取 id(假设您将 id 列作为第一列获取,这通常是这种情况)。 However, note that the id is passed in as part of the signature (see the last argument in public void onListItemClick(ListView l, View v, int position, long id) ) so you really don't need to fetch it again (but you certainly can if you want to burn the cycles).但是,请注意 id 作为签名的一部分传入(请参阅public void onListItemClick(ListView l, View v, int position, long id)中的最后一个参数)所以你真的不需要再次获取它(但是如果你想烧掉循环,你当然可以)。 For accessing other columns you can do the same thing, just change the column index.要访问其他列,您可以执行相同的操作,只需更改列索引。

Hope that helps.希望有帮助。

Another way:另一种方式:

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
            }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM