Лучший способ сделать это - использовать CursorAdapter или SimpleCursorAdapter.Это даст вам лучшую производительность, и как только вы поймете это, вы обнаружите, что это самый простой подход при использовании базы данных SQLite.
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
Ниже приведен простой CustomCursorAdapter, который я используючасто.Просто добавьте класс CustomCursorAdapter как внутренний класс.
protected class CustomCursorAdapter extends SimpleCursorAdapter {
private int layout;
private LayoutInflater inflater;
private Context context;
public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.i("NewView", newViewCount.toString());
View v = inflater.inflate(R.layout.list_cell, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
//1 is the column where you're getting your data from
String name = c.getString(1);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.textView);
if (name_text != null) {
name_text.setText(name);
}
}
Создайте экземпляр CustomCursorAdapter, например, так ... Вам нужно будет создать курсор точно так же, как вы уже это делаете.
protected String[] from;
protected int[] to;
//From is the column name in your cursor where you're getting the data
//to is the id of the view it will map to
from = new String[]{"name"};
to = new int[]{R.id.textView};
CustomCursorAdapter adapter = new CustomCursorAdapter(this, R.layout.list, cursor, from, to);
listView.setAdapter(adapter);