我正在尝试列出MainActivity中插入数据库的数据的ListView。
我创建了数据库,并在MainActivity中插入了ArrayAdapter和ListView。
我的问题是,当我启动它时,应用程序崩溃了,我无法弄清楚缺少什么。
MainActivity代码:
public class MainActivity extends ActionBarActivity{
final Context context =this;
ArrayAdapter<String> arrayAdapter;
ArrayList<String>listItems = new ArrayList<String>();
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotesDbAdapter entry = new NotesDbAdapter(MainActivity.this);
List<String> all = entry.getAllCategory();
if (all.size()>0){
lv = (ListView) findViewById(R.id.list_view_notes);
arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, all);
lv.setAdapter(arrayAdapter);
}else
{
Toast.makeText(MainActivity.this, "No Items to display", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.action_addNotes:
Intent addNotes = new Intent(MainActivity.this, AddNotes.class);
startActivity(addNotes);
}
return super.onOptionsItemSelected(item);
}
}
我的数据库代码:
public class NotesDbAdapter extends SQLiteOpenHelper {
private NotesDbAdapter ourHelper;
private SQLiteDatabase ourDatabase;
private Context ourContext;
public static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null);";
public NotesDbAdapter(Context context){
super(context, TableInfo.TableData.DATABASE_NAME, null, DATABASE_VERSION);
// a- putting a log message so it will tell us what is happening in the code
Log.d("Database Operations", "Database Created");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
Log.d("onCreate", "onCreate() database");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
/*
db.execSQL("DROP TABLE IF EXISTS note");
onCreate(db);
Log.d("onUpgrade", "onUpdate() database");
*/
}
public void putInformation(NotesDbAdapter dop, String title, String body){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableInfo.TableData.COLUMN_TITLE, title);
cv.put(TableInfo.TableData.COLUMN_BODY, body);
SQ.insert(TableInfo.TableData.TABLE_NAME, null, cv);
Log.d("Database Operations", "One Row Inserted");
}
public List<String> getAllCategory(){
List<String> List = new ArrayList<String>();
String selectQuery = "SELECT * FROM" + TableInfo.TableData.TABLE_NAME;
Cursor cursor = ourDatabase.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do {
List.add(cursor.getString(1));
}while (cursor.moveToNext());
}
return List;
}
}
任何人都可以检查我上面的代码,并帮助修复它吗?
提前谢谢。
答案 0 :(得分:0)
您需要使用SimpleCursorAdapter,这是使用数据库的最佳做法ContentProvider + CursorLoader vogella
答案 1 :(得分:0)
下面一行应该在单词FROM之后有一个空格。目前它不是一个有效的声明。
我不能说这是否是你唯一的问题,但它是第一个没有看到堆栈跟踪而脱颖而出的问题。
String selectQuery = "SELECT * FROM" + TableInfo.TableData.TABLE_NAME;