Есть два активити. В одном заполняется два поля, затем они через Extras
передаются в первое активити. В первом находится listview
, который должен отображать данные из базы. Что может быть не так?
public class MainActivity extends AppCompatActivity {
EditText etFindNote;
ListView list;
Button btnAdd, btnMenu;
final int REQUEST_CODE = 1;
SimpleCursorAdapter scAdapter;
Cursor cursor;
SQLiteDatabase db;
final String ATTRIBUTE_DATE = "date";
final String ATTRIBUTE_NOTE = "note";
DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etFindNote = (EditText) findViewById(R.id.etFindNote);
list = (ListView) findViewById(R.id.list);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnMenu = (Button) findViewById(R.id.btnMenu);
dbHelper = new DBHelper(this);
db = dbHelper.getWritableDatabase();
String[] from = new String[] {"data", "note"};
int[] to = new int[]{R.id.itemDate, R.id.itemNote};
cursor = db.query("mytable", from, null, null, null, null, null);
scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to);
list.setAdapter(scAdapter);
registerForContextMenu(list);
}
public void NewNoteClick (View v) {
Intent intent = new Intent (this, AddActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
protected void onActivityResult (int requestCode, int resultCode, Intent intent){
if (requestCode == REQUEST_CODE){
if (resultCode == RESULT_OK) {
ContentValues cv = new ContentValues();
db = dbHelper.getWritableDatabase();
cv.put("data", intent.getStringExtra("data"));
cv.put("note", intent.getStringExtra("note"));
db.insert("myDB", null, cv);
}
}
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper (Context context) {
super(context, "myDB", null, 1);
}
public void onCreate (SQLiteDatabase db){
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement,"
+ "date text,"
+ "note text"+");");
}
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
}
}
}
Логи
01-11 23:21:57.930 28125-28125/? E/myLogs: определяем элементы
01-11 23:21:57.930 28125-28125/? E/myLogs: создаем бд
01-11 23:21:57.940 28125-28125/? E/myLogs: создаем курсор для чтения
01-11 23:21:57.940 28125-28125/? E/myLogs: определяем массивы для чтения
01-11 23:21:57.940 28125-28125/? E/myLogs: Считывам базу дннх в логи
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 1, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 2, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 3, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 4, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 5, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 6, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 7, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 8, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 9, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: id = 10, date = 07.05.2017, note = Hello
01-11 23:21:57.940 28125-28125/? E/myLogs: Закончили считывать в логи
01-11 23:21:57.940 28125-28125/? E/myLogs: передаем курсор адаптеру
01-11 23:21:57.950 28125-28125/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alex.notes, PID: 28125
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alex.notes/com.example.alex.notes.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2448)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.access$1200(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1371)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5595)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.widget.CursorAdapter.init(CursorAdapter.java:172)
at android.widget.CursorAdapter.<init>(CursorAdapter.java:149)
at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
at com.example.alex.notes.MainActivity.onCreate(MainActivity.java:87)
at android.app.Activity.performCreate(Activity.java:6100)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.access$1200(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1371)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5595)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)