在 ListView 中显示数据库数据
Displaying the Database data in a ListView
我试图在 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;
}
}
任何人都可以检查我上面的代码并帮助修复它吗?
提前谢谢你。
您需要使用 SimpleCursorAdapter,使用数据库的最佳实践使用 ContentProvider+CursorLoader vogella
下一行的 FROM 后面应该有一个 space。目前这不是一个有效的声明。
我不能说这是不是你唯一的问题,但这是第一个没有看到堆栈跟踪的突出问题。
String selectQuery = "SELECT * FROM" + TableInfo.TableData.TABLE_NAME;
我试图在 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;
}
}
任何人都可以检查我上面的代码并帮助修复它吗?
提前谢谢你。
您需要使用 SimpleCursorAdapter,使用数据库的最佳实践使用 ContentProvider+CursorLoader vogella
下一行的 FROM 后面应该有一个 space。目前这不是一个有效的声明。
我不能说这是不是你唯一的问题,但这是第一个没有看到堆栈跟踪的突出问题。
String selectQuery = "SELECT * FROM" + TableInfo.TableData.TABLE_NAME;