如何使用游标Loader来访问检索到的数据? [英] How to use Cursor Loader to access retrieved data?

查看:102
本文介绍了如何使用游标Loader来访问检索到的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑如何使用游标来访问,我需要使用CursorLoader模型的数据。我能够使用SimpleCursorAdapter,光标传递给你必须实现如下onLoadFinished方法中适配器的swapCursor方法。

I'm a little confused about how to use the cursor to access the data that I need using the CursorLoader model. I was able to use a SimpleCursorAdapter and pass the cursor to the swapCursor method of the adapter within the onLoadFinished method that you have to implement as follows.

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

    facilityAdapter.swapCursor(cursor);

}

现在我需要查询数据库,并根据行数显示返回一个整数。我如何获取这些信息?有一些信息,我想存储选择,这样我可以捆绑起来,并把它传递到下一个活动的某些行(_id的),因为id是在其他数据库表的外键,我会需要查询数据库。在Android开发者网站,我看到有accesor方法光标类,如cursor.getInt,cursor.getString,等等等等,我应该用这些来访问,我之后的数据。

Now I need to query the database and based on the number of rows returned display an integer. How do I access this information? There is some information that I would like to store (the _id) of certain rows selected so that I can bundle it up and pass it to the next activity, because the id is a foreign key in other database tables and I'll need to to query the database. On the android developer site I've seen that there are accesor methods to the cursor class such as cursor.getInt, cursor.getString, etc etc, should I be using these to access the data that I am after.

推荐答案

好像你有两个问题在这里...所以我会回答他们。纠正我,如果我误解你的问题...

It seems like you have two questions here... so I'll answer them both. Correct me if I misunderstood your question...


  1. 要获取表中的行数,则按照以下方法执行一个简单的查询:

  1. To get the number of rows in the table, you perform a simple query as follows:

Cursor cur = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
int numColumns = cur.getCount();
cur.close();


  • 检索所选项目的ID,其实很简单,由于到Android适配器。下面是概述了如何你可能会传递一个id为另一个活动(例如,单击一个ListView项目时)的例子。

  • Retrieving the id of a selected item is actually very simple, thanks to Android adapters. Here's an example that outlines how you might pass an id to another Activity (for example, when a ListView item is clicked).

    public class SampleActivity extends Activity implements
            LoaderManager.LoaderCallbacks<Cursor> {
    
        // pass this String to the Intent in onListItemClicked()
        public static final String ID_TAG = "id_tag";
    
        private SimpleCursorAdapter mAdapter;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            /* setup layout, initialize the Loader and the adapter, etc. */
    
            setListAdapter(mAdapter);
        } 
    
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            final Intent i = new Intent(getApplicationContext(), NewActivity.class);
            i.putExtra(ID_TAG, id);
            // pass the id to NewActivity. You can retrieve this information in
            // the Activity's onCreate() method with something like:
            // long id = getIntent().getLongExtra(SampleActivity.ID_TAG, -1);
            startActivity(i);
        }
    }
    

    您需要创建一个自己的URI查询国外数据库。这可以用类似进行:

    You'll have to create a URI yourself to query the foreign database. This can be done with something like:

    Uri uri = Uri.withAppendedPath(CONTENT_URI, String.valueOf(id));
    

    其中, CONTENT_URI 是适当的CONTENT_URI你的的ContentProvider

    where CONTENT_URI is the appropriate CONTENT_URI for your ContentProvider.

    让我知道,如果有帮助!

    Let me know if that helps!

    这篇关于如何使用游标Loader来访问检索到的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆