<学习笔记>Android使用Loader加载sqlite数据到ListView
2016-12-17 13:49
471 查看
Android官方现在推荐使用加载器为ListView填充数据。
官方使用的例子是使用CursorLoader加载通讯录联系人,如需加载自己的数据,需要定义自己的Loader。
1.布局文件:
main.xml:
<ListView
android:id="@+id/list"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#12000000"
android:dividerHeight="1dp"/>
item布局list_item.xml:
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<TextView android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"/>
<TextView android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
</LinearLayout>
2.自定义Loader,继承自AsyncTaskLoader,覆写loadInBackground方法:
DataLoader.java:
public class DataLoader extends AsyncTaskLoader<Cursor> {
public DataLoader(Context context) {
super(context);
}
@Override
public Cursor loadInBackground() {
//连接数据库获取所有项目数据
DBHelper dbHelper = new DBHelper(getContext());
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor mData = db.query(“表名”,
new String[]{"rowid _id",“字段1”, “字段2”, “字段3”},
null, null, null, null, null);
return mData;
}
}DBHelper继承自SQLiteOpenHelper,关于sqlite数据库查询操作不再赘述。
注:返回的Cursor要求必须有"_id"字段,如果你的数据库中没有设计这个字段也不要紧,如上加"rowid _id"就可以。
3.MainActivity实现LoaderManager.LoaderCallbacks<Cursor>接口
MainActivity.java:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
private ListView listView;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
String[] fromColumns = {"字段1","字段2","字段3"};
int[] toViews = {R.id.one, R.id.two, R.id.three};
listView = (ListView) findViewById(R.id.list);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, null,
fromColumns,
toViews, 0);
listView.setAdapter(adapter);
getLoaderManager().initLoader(0, null, this).forceLoad();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Toast.makeText(this, "加载中...", Toast.LENGTH_SHORT).show();
return new DataLoader(MainActivity.this);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Toast.makeText(this, "加载完成", Toast.LENGTH_SHORT).show();
adapter.swapCursor(data);//将数据绑定到adapter中
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}注:Android官方例子中只给出了getLoaderManager().initLoader(0, null, this)初始化,但是这样只会执行onCreateLoader,并不会执行到DataLoader的loadInBackground函数,从而也不会执行到onLoadFinished函数,只是初始化了加载器,并没有加载数据。因此要用getLoaderManager().initLoader(0, null, this).forceLoad();强制加载。这方面Android的官方文档说得也不是很清楚,网上有很多剖析LoaderManager源码的可以参考。
另:关于Loader加载器的原理方法不再另述,可参考下文章,对我蛮有帮助。
http://blog.csdn.net/iispring/article/details/48834767 http://www.jianshu.com/p/385327e35711
官方使用的例子是使用CursorLoader加载通讯录联系人,如需加载自己的数据,需要定义自己的Loader。
1.布局文件:
main.xml:
<ListView
android:id="@+id/list"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#12000000"
android:dividerHeight="1dp"/>
item布局list_item.xml:
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<TextView android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"/>
<TextView android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
</LinearLayout>
2.自定义Loader,继承自AsyncTaskLoader,覆写loadInBackground方法:
DataLoader.java:
public class DataLoader extends AsyncTaskLoader<Cursor> {
public DataLoader(Context context) {
super(context);
}
@Override
public Cursor loadInBackground() {
//连接数据库获取所有项目数据
DBHelper dbHelper = new DBHelper(getContext());
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor mData = db.query(“表名”,
new String[]{"rowid _id",“字段1”, “字段2”, “字段3”},
null, null, null, null, null);
return mData;
}
}DBHelper继承自SQLiteOpenHelper,关于sqlite数据库查询操作不再赘述。
注:返回的Cursor要求必须有"_id"字段,如果你的数据库中没有设计这个字段也不要紧,如上加"rowid _id"就可以。
3.MainActivity实现LoaderManager.LoaderCallbacks<Cursor>接口
MainActivity.java:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
private ListView listView;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
String[] fromColumns = {"字段1","字段2","字段3"};
int[] toViews = {R.id.one, R.id.two, R.id.three};
listView = (ListView) findViewById(R.id.list);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, null,
fromColumns,
toViews, 0);
listView.setAdapter(adapter);
getLoaderManager().initLoader(0, null, this).forceLoad();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Toast.makeText(this, "加载中...", Toast.LENGTH_SHORT).show();
return new DataLoader(MainActivity.this);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Toast.makeText(this, "加载完成", Toast.LENGTH_SHORT).show();
adapter.swapCursor(data);//将数据绑定到adapter中
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}注:Android官方例子中只给出了getLoaderManager().initLoader(0, null, this)初始化,但是这样只会执行onCreateLoader,并不会执行到DataLoader的loadInBackground函数,从而也不会执行到onLoadFinished函数,只是初始化了加载器,并没有加载数据。因此要用getLoaderManager().initLoader(0, null, this).forceLoad();强制加载。这方面Android的官方文档说得也不是很清楚,网上有很多剖析LoaderManager源码的可以参考。
另:关于Loader加载器的原理方法不再另述,可参考下文章,对我蛮有帮助。
http://blog.csdn.net/iispring/article/details/48834767 http://www.jianshu.com/p/385327e35711
相关文章推荐
- Android_Loader_使用LoaderManager管理Loader实现异步动态加载数据
- [置顶] Android_ListView_Adapter使用和数据动态加载
- [置顶] Android_ListView_Adapter使用和数据动态加载
- android学习记录3(数据存储的方式:sqlite、sp、存文件。listview简单使用)
- Android_Loader_使用LoaderManager管理Loader实现异步动态加载数据
- Android-使用ListView把SQLite中的数据显示至屏幕
- Android列表组件ListView使用详解之动态加载或修改列表数据
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- [置顶] Android_Loader_使用LoaderManager管理Loader实现异步动态加载数据
- Android 下分批加载数据以及listView使用过程中的优化
- Android中使用Listview动态加载数据
- Android_Loader_使用LoaderManager管理Loader实现异步动态加载数据
- Android开发ListView使用OnScrollListener实现分页加载数据
- Android 使用Android-Universal-Image-Loader进行异步数据加载
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- Android_ListView_Adapter使用和数据动态加载
- Day07-Android中SQLite数据存储,ListView的基本使用
- android 使用SimpleCursorAdapter将SQLite数据显示到ListView
- Android控件使用—Listview加载网络数据、图片并跳转传值
- Android-使用ListView把SQLite中的数据显示至屏幕