<学习笔记>Android使用Loader加载sqlite数据到ListView

来源:互联网 发布:新还珠格格知乎 编辑:程序博客网 时间:2024/05/22 04:50

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



0 0