Начал делать CursorAdapter
по этому примеру
Я думаю, что все правильно, но появилась ошибка:
FATAL EXCEPTION: main java.lang.IllegalArgumentException: column '_id' does not exist at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303) at android.widget.CursorAdapter.init(CursorAdapter.java:168) at android.widget.CursorAdapter.(CursorAdapter.java:116) at db.model.ListAdapter.(ListAdapter.java:17) at com.vitaliy.usedb.MainActivity.onClick(MainActivity.java:91) at android.view.View.performClick(View.java:4244) at android.view.View$PerformClick.run(View.java:17635) at android.os.Handler.handleCallback(Handler.java:800) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5400) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) at dalvik.system.NativeStart.main(Native Method)*
Класс для работы с БД, расширяющий SQLiteOpenHelper
public class DBHelper extends SQLiteOpenHelper{
private final String LOG = "####MY_DB_LOG####";
public static final String TABLE = "mytable";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_EMAIL = "email";
private String CreateTable = "create table " +TABLE + " ( "
+ COLUMN_ID +" integer primary key autoincrement,"
+ COLUMN_NAME +" text not null, "
+ COLUMN_EMAIL +" text " + ")";
public DBHelper(Context context) {
super(context, "myDb.db", null, 1);
Log.d(LOG, "create database");
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
Log.d(LOG, "create table");
sqLiteDatabase.execSQL(CreateTable);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
Класс расширяющий CursorAdapter:
public class ListAdapter extends CursorAdapter {
public ListAdapter(Context context, Cursor cursor, int flags){
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return LayoutInflater.from(context).inflate(R.layout.item_data_list, viewGroup, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView id = (TextView) view.findViewById(R.id.id_list_item);
TextView name = (TextView) view.findViewById(R.id.name_list_item);
TextView email = (TextView) view.findViewById(R.id.email_list_item);
int intId = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID));
String strName = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NAME));
String strEmail = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_EMAIL));
id.setText(String.valueOf(intId));
name.setText(strName);
email.setText(strEmail);
}
}
И применение CursorAdapter в главном активити:
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + DBHelper.TABLE ,null);
ListAdapter listAdapter = new ListAdapter(MainActivity.this , cursor, 0);
listView.setAdapter(listAdapter);
В чем может быть проблема?