SimpleAdapter,CursorAdapter,SimpleCursorAdapter
SimpleAdapter,CursorAdapter,SimpleCursorAdapter三者的使用区别
布局文件
- 布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<!--数据库字段名-->
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="id"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="name"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="age"/>
</LinearLayout>
<!--数据库内容-->
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/linearLayout">
</ListView>
</RelativeLayout>
- ListView中item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!--id-->
<TextView
android:id="@+id/tv_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="id"/>
<!--姓名-->
<TextView
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"/>
<!--年龄-->
<TextView
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"/>
</LinearLayout>
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<Map<String, Object>> getData() {
List<Map<String, Object>> 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<String ,Object> 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);
}
网友评论