Android 在列表视图中显示数据库数据

Android 在列表视图中显示数据库数据,android,database,android-listview,android-sqlite,Android,Database,Android Listview,Android Sqlite,我试图在MainActivity中列出数据库中插入的数据的列表视图 我已经创建了数据库,并在MainActivity中插入了ArrayAdapter和ListView 我的问题是,当我启动应用程序时,它正在崩溃,我无法找出丢失了什么 主要活动代码: public class MainActivity extends ActionBarActivity{ final Context context =this; ArrayAdapter<String> arrayA

我试图在MainActivity中列出数据库中插入的数据的列表视图

我已经创建了数据库,并在MainActivity中插入了ArrayAdapter和ListView

我的问题是,当我启动应用程序时,它正在崩溃,我无法找出丢失了什么

主要活动代码:

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;
    }



}
有人可以检查我上面的代码,并帮助修复它吗


提前感谢。

您需要使用SimpleCorsorAdapter,这是使用数据库的最佳实践使用ContentProvider+CursorLoader

下面的一行应该在单词FROM后留出一个空格。目前这不是一个有效的声明

我不能说这是不是你唯一的问题,但这是第一个没有任何痕迹的问题

String selectQuery = "SELECT * FROM" + TableInfo.TableData.TABLE_NAME;