[置顶] AndroidUI--SimpleAdapter CursorAdapter

使用SimpleAdapter、CursorAdapter读取数据库中的数据并将其显示到listview上。

SimpleCursorAdapter – Similar to an ArrayAdapter because it can be used without subclassing. Simply provide the required parameters (such as a cursor and layout information) in the constructor and then assign to a ListView.(SimpleCursorAdapter继承了CursorAdapter,用法类似SimpleAdapter)

CursorAdapter – A base class that you can inherit from when you need more control over the binding of data values to layout controls (for example, hiding/showing controls or changing their properties).(Cursor必须要有个命名为”_id”的列)

CursorAdapter必须实现以下函数:
abstract View newView(Context context, Cursor cursor, ViewGroup parent)
Makes a new view to hold the data pointed to by cursor.
abstract void bindView(View view, Context context, Cursor cursor)
Bind an existing view to the data pointed to by cursor

newView该函数第一次回调用后,如果数据增加后也会再调用,但是重绘是不会调用的。
数据增加后,回调用该函数来生成与新增数据相对应的view。
bindView函数第一次回调用后,如果数据更新也会再调用,但重绘会再次调用的。
在调用bindView如果发现view为空会先调用newView来生成view

首先创建数据库并向其中插入数据:
MysqliteOpenHelper.java

public class MysqliteOpenHelper extends SQLiteOpenHelper {

    public static final String CREATE_TABLE_SQL = "CREATE TABLE vegetables (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT)";

    public static final String Databasename = "vegetables.db";
    public static final int DatabaseVersion = 1;

    public MysqliteOpenHelper(Context context){
        super(context,Databasename,null,DatabaseVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_SQL);
        //insert data
        db.execSQL("insert into vegetables (name) values ('Vegetables')");
        db.execSQL("insert into vegetables (name) values ('Fruits')");
        db.execSQL("insert into vegetables (name) values ('Flowers Buds')");
        db.execSQL("insert into vegetables (name) values ('Legumes')");
        db.execSQL("insert into vegetables (name) values ('Bulbs')");
        db.execSQL("insert into vegetables (name) values ('Tubers')");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        //do nothing until second version
    }
}

CursorAdapterActivity.java
首选是使用CursorAdapter的用法:

public class CursorAdapterActivity extends ListActivity {

    public MysqliteOpenHelper dbHelper;
    Cursor cursor;
    SimpleCursorAdapter mAdapter;
    ListView listView;
    TextView textView;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        dbHelper = new MysqliteOpenHelper(this);

        SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();
        cursor = sqLiteDatabase.rawQuery("SELECT * FROM vegetables",null);
        startManagingCursor(cursor);

        listView = getListView();
        listView.setAdapter(new HomeCursorAdapter(this,cursor));
    }

    public class HomeCursorAdapter extends CursorAdapter{

        public HomeCursorAdapter(Context context,Cursor cursor){
            super(context,cursor);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
            //取得布局与控件
            LayoutInflater inflater = getLayoutInflater();
            View inflate = inflater.inflate(R.layout.listview_item,null);
            textView = (TextView) inflate.findViewById(R.id.tv_name);
            return inflate;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            //绑定数据
            String name = cursor.getString(cursor.getColumnIndex("name"));
            textView.setText(name);
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        stopManagingCursor(cursor);
        cursor.close();
    }
}

若是使用SimpleCursorAdapter,可以简化不少代码:

public class CursorAdapterActivity extends ListActivity {

    public MysqliteOpenHelper dbHelper;
    Cursor cursor;
    SimpleCursorAdapter mAdapter;
    ListView listView;
    TextView textView;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        dbHelper = new MysqliteOpenHelper(this);

        SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();
        cursor = sqLiteDatabase.rawQuery("SELECT * FROM vegetables",null);
        startManagingCursor(cursor);
        //列名与控件id一一对应
        String[] fromColumns = new String[]{"name"};
        int[] toControlIDs = new int[]{R.id.tv_name};
        mAdapter = new SimpleCursorAdapter(CursorAdapterActivity.this,R.layout.listview_item,cursor,fromColumns,toControlIDs);
        listView = getListView();
        listView.setAdapter(mAdapter);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        stopManagingCursor(cursor);
        cursor.close();
    }
}

[置顶] AndroidUI--SimpleAdapter CursorAdapter_第1张图片

你可能感兴趣的:([置顶] AndroidUI--SimpleAdapter CursorAdapter)