使用 SQLite 数据库实现 ListFragment 的最佳方法是什么。目前我已经创建了一个 DBAdapter 来促进打开、关闭和获取记录到 SimpleCursorAdapter 中。我有我的 MainActivity,它实现了一个带有导航选项卡的 ActionBar,我想为每个选项卡显示不同的 ListView。
这是我的 ListFragment:
public class MaterialsListFragment extends ListFragment {
public DBAdapter db;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.portrait_material_view, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create new DBAdapter
db = new DBAdapter(getActivity());
db.open();
Cursor c = db.getAllRecords();
String[] from = new String[] { DBAdapter.KEY_IDNO, DBAdapter.KEY_MATERIAL };
int[] to = new int[] { R.id.idno, R.id.materials };
// Now create an array adapter and set it to display using our row
MyListAdapter materials = new MyListAdapter(this, R.layout.list_cell, c, from, to);
setListAdapter(materials);
}
这是 MyListAdapter 代码,目前位于它自己的类文件中:
public class MyListAdapter extends SimpleCursorAdapter {
public MyListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
super(context, layout , cursor, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Create the idno textview with background image
TextView idno = (TextView) view.findViewById(R.id.idno);
idno.setText(cursor.getSring(3));
// create the material textview
TextView materials = (TextView) view.findViewById(R.id.materials);
materials.setText(cursor.getString(1));
}
}
这是解决这个问题的方法吗?如果您有任何建议,或者您知道任何好的例子,我将不胜感激。