android 自定义simplecursoradapter,SimpleAdapter,CursorAdapter,SimpleCursorAdapter

SimpleAdapter,CursorAdapter,SimpleCursorAdapter

SimpleAdapter,CursorAdapter,SimpleCursorAdapter三者的使用区别

布局文件

布局文件

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.vv.zvv.SimpleAdapterActivity">

android:id="@+id/linearLayout"

android:layout_width="match_parent"

android:layout_height="20dp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:gravity="center"

android:text="id"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:gravity="center_vertical"

android:text="name"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:gravity="center_vertical"

android:text="age"/>

android:id="@+id/lv"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@+id/linearLayout">

ListView中item布局

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

android:id="@+id/tv_id"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:gravity="center"

android:text="id"/>

android:id="@+id/tv_name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:gravity="center_vertical"

android:text="name"/>

android:id="@+id/tv_age"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:gravity="center_vertical"

android:text="age"/>

SimpleAdapter

摘要

SimpleAdapter simpleAdapter = new SimpleAdapter(

this,

getData(),

R.layout.item_database,

new String[]{"_id", "name", "age"},

new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age});

详细使用

private void init() {

//打开数据库

File file = new File(Environment.getExternalStorageDirectory(), "my_database.db");

SQLiteDatabase database = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);

mCursor = database.query("table_person", new String[]{"_id", "name", "age"}, "age >= ?", new String[]{"0"}, null, null, null);

//实例适配器

SimpleAdapter simpleAdapter = new SimpleAdapter(this,getData(), R.layout.item_database,

new String[]{"_id", "name", "age"}, new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age});

//绑定适配器

mListView.setAdapter(simpleAdapter);

}

private List> getData() {

List> list = new ArrayList<>();

int idIndex = mCursor.getColumnIndex("_id");

int nameIndex = mCursor.getColumnIndex("name");

int ageIndex = mCursor.getColumnIndex("age");

while (mCursor.moveToNext()) {

int id = mCursor.getInt(idIndex);

String name = mCursor.getString(nameIndex);

int age = mCursor.getInt(ageIndex);

Map map = new HashMap<>();

map.put("_id",id);

map.put("name",name);

map.put("age",age);

list.add(map);

}

mCursor.close();

return list;

}

SimpleCursorAdapter

摘要

SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(

this,

R.layout.item_database,

cursor,

new String[]{"_id","name", "age"},

new int[]{R.id.tv_id,R.id.tv_name, R.id.tv_age},

CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

详细使用

private void init() {

File file = new File(Environment.getExternalStorageDirectory(), "my_database.db");

SQLiteDatabase database = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);

Cursor cursor = database.query("table_person", new String[]{"_id", "name", "age"}, "age >= ?", new String[]{"0"}, null, null, null);

// flags:FLAG_REGISTER_CONTENT_OBSERVER,官方推荐使用的标签,当数据发生变化时,通过观察者标签,实时观察数据的变化,并刷新UI界面

SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(

this, R.layout.item_database, cursor, new String[]{"_id","name", "age"}, new int[]{R.id.tv_id,R.id.tv_name, R.id.tv_age}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

mListView.setAdapter(simpleCursorAdapter);

}

CursorAdapter

摘要

MyCursorAdapter adapter = new MyCursorAdapter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

自定义MyCursorAdapter

public class MyCursorAdapter extends CursorAdapter {

public MyCursorAdapter(Context context, Cursor c, int flags) {

super(context, c, flags);

}

@Override

public View newView(Context context, Cursor cursor, ViewGroup parent) {

View view = LayoutInflater.from(context).inflate(R.layout.item_database, null);

ViewHolder viewHolder = new ViewHolder();

viewHolder.mIdTextView = (TextView) view.findViewById(R.id.tv_id);

viewHolder.mAgeTextView = (TextView) view.findViewById(R.id.tv_age);

viewHolder.mNameTextView = (TextView) view.findViewById(R.id.tv_name);

view.setTag(viewHolder);

return view;

}

@Override

public void bindView(View view, Context context, Cursor cursor) {

ViewHolder viewHolder = (ViewHolder) view.getTag();

int id = cursor.getInt(cursor.getColumnIndex("_id"));

String name = cursor.getString(cursor.getColumnIndex("name"));

int age = cursor.getInt(cursor.getColumnIndex("age"));

viewHolder.mIdTextView.setText(id + "");

viewHolder.mNameTextView.setText(name);

viewHolder.mAgeTextView.setText(age + "");

}

class ViewHolder {

TextView mIdTextView;

TextView mAgeTextView;

TextView mNameTextView;

}

}

使用

private void init() {

//拿到数据

File file = new File(Environment.getExternalStorageDirectory(), "my_database.db"); // 数据库文件 /storage/emulated/0/my_database.db

SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);

//读取数据

Cursor cursor = sqLiteDatabase.query("table_person", new String[]{"_id", "name", "age"}, "age >= ?", new String[]{"0"}, null, null, null);

//适配

MyCursorAdapter adapter = new MyCursorAdapter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

mListView.setAdapter(adapter);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值