得到的ListView选择的项目绑定SimpleCursorAdapter绑定、项目、ListView、SimpleCursorAdapter

2023-09-12 23:54:59 作者:指着这、说疼

我是全新的Andr​​oid开发...来自iPhone和净背景的。我见过非常类似的问题这一个,但他们没有处理的SimpleCursorAdapter。

I'm brand new to Android development... coming from iPhone and .Net background. I've seen very similar questions to this one, but none of them dealt with the SimpleCursorAdapter.

我有一个基本的ListActivity它使用游标从SQLite的查询数据绑定到我的ListView:

I have a basic ListActivity which uses a Cursor to bind data from a SQLite query to my 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);
}

这是正确的方式来获得的元素所选择的ID?这似乎很奇怪,因为我不认为我可以用我的光标之后该数据库被关闭(这是为我绑定后)。什么是通过在id的点,当我似乎无法找到获得从ID的实际项目的一种方式?另外,我不明白为什么getItemAtPosition()将返回一个指针......光标绑定到整个列表;不只是一行。最后,如​​果这是正确的,是有越来越显示光标的两种方法之间的差异?谢谢你。

Is this the proper way to get the id of the element that was selected? It seems strange, because I wouldn't think I could use my cursor after the database is closed (which is is after I bind). 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? Also, I don't understand why the getItemAtPosition() would return a cursor... the cursor is bound to the entire list; not just one row. Finally, if this is correct, is there a difference between the two ways shown of getting the cursor? Thanks.

推荐答案

那么几个百分点:你取的光标后,要呼叫 startManagingCursor 。这关系游标的生命周期与活动的生命周期(所以当活动被破坏光标被关闭/清理)。

So a couple of points: after you fetch the cursor, you want to call startManagingCursor. This ties the cursor's lifecycle with Activity's lifecycle (so when the Activity gets destroyed the cursor gets closed/cleaned up).

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可以滚动,做这种性质的东西,可能需要未来进入光标的内容的实时连接

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.

要你的核心问题,做到在 onListItemClick 最简单的方法是这样的:

To your core question, the easiest way to do it in onListItemClick is this:

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

您可以用 c.getLong(0)以获得ID(假设你取的ID列这是一般情况下的第一列)。但是,请注意,该ID传递作为签名的一部分(见的最后一个参数公共无效onListItemClick(ListView的L,视图V,INT位置,长ID))所以你真的不需要再次获取它(但你肯定可以,如果你要刻录的周期)。对于访问等栏目,你可以做同样的事情,只是改变了列索引。

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). 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). For accessing other columns you can do the same thing, just change the column index.

希望有所帮助。