光标适配器和SQLite的例子光标、适配器、例子、SQLite

由网友(拿根辣条砸死你)分享简介:您好我在寻找样品code,其中光​​标适配器使用SQLite的?Hello I am looking for sample code in which cursor adapter is used with sqlite?推荐答案下面是一个非常简单的,但是非常有效,例如。一旦你的基本下跌,你可以很容易地建立过...

您好 我在寻找样品code,其中光​​标适配器使用SQLite的?

Hello I am looking for sample code in which cursor adapter is used with sqlite?

推荐答案

下面是一个非常简单的,但是非常有效,例如。一旦你的基本下跌,你可以很容易地建立过它。

Really simple example.

Here is a really simple, but very effective, example. Once you have the basics down you can easily build off of it.

有两个主要部分使用的光标适配器使用SQLite:

There are two main parts to using a Cursor Adapter with SQLite:

创建一个从数据库适当的光标

创建自定义的光标适配器,是以光标从数据库中对用数据的查看您打算重新present与数据。

Create a custom Cursor Adapter that takes the Cursor data from the database and pairs it with the View you intend to represent the data with.

在你的活动:

SQLiteOpenHelper sqLiteOpenHelper = new SQLiteOpenHelper( 
        context, DATABASE_NAME, null, DATABASE_VERSION);

SQLiteDatabase sqLiteDatabase = sqLiteOpenHelper.getReadableDatabase();

String query = "SELECT * FROM clients ORDER BY company_name ASC"; // No trailing ';'

Cursor cursor = sqLiteDatabase.rawQuery(query, null); 

ClientCursorAdapter adapter = new ClientCursorAdapter(
        this, R.layout.clients_listview_row, cursor, 0 );

this.setListAdapter(adapter);

2。创建自定义光标适配器。

注:从 ResourceCursorAdapter 扩展假设你使用XML来创建你的意见的

2. Create a Custom Cursor Adapter.

Note: Extending from ResourceCursorAdapter assumes you use XML to create your views.

public class ClientCursorAdapter extends ResourceCursorAdapter {

    public ClientCursorAdapter(Context context, int layout, Cursor c, int flags) {
        super(context, layout, c, flags);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView name = (TextView) view.findViewById(R.id.name);
        name.setText(cursor.getString(cursor.getColumnIndex("name")));

        TextView phone = (TextView) view.findViewById(R.id.phone);
        phone.setText(cursor.getString(cursor.getColumnIndex("phone")));
    }
}
阅读全文

相关推荐

最新文章