访问SQLite数据库行

时间:2015-08-11 12:16:27

标签: java android sqlite

我正在为我的数据库的一些代码挣扎,我设法访问我点击的项目的ID号但是我无法访问任何更多的数据?我怎么能这样做?

例如现在我有我的id号码我想在一个单独的窗口中显示该行的所有数据但我似乎无法用光标拉数据

String itemselect = String.valueOf(spinner.getSelectedItem());
Toast.makeText(Developer.this, itemselect, Toast.LENGTH_LONG).show();
final Cursor cursor = mydb.getAllRows(itemselect);
startManagingCursor(cursor);

String[] fromfieldnames = new String[]{
            DatabaseHelper.COL_1, DatabaseHelper.COL_2, DatabaseHelper.COL_3, 
            DatabaseHelper.COL_4, DatabaseHelper.COL_5, DatabaseHelper.COL_6 };
int[] toviewids = new int[]{R.id.textone, R.id.texttwo, R.id.textthree, R.id.textfour, R.id.textfive, R.id.textsix};
final SimpleCursorAdapter mycursoradapter = new SimpleCursorAdapter(this, R.layout.listtext, cursor, fromfieldnames, toviewids);
listView.setAdapter(mycursoradapter);

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Long string = mycursoradapter.getItemId(position);
        Toast.makeText(Developer.this, string.toString(),Toast.LENGTH_LONG).show();
        return true;
    }
});

此代码中没有错误我只需要尝试添加此额外功能

先谢谢你。

3 个答案:

答案 0 :(得分:1)

以下列方式使用光标:

public Cursor getRowByID(int ID) {

    SQLiteDatabase db = this.getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String [] sqlSelect = {"id, col_1, col_2, col_3"}; 
    String sqlTables = "your_table";
    String filter = "id= "+ID;

    qb.setTables(sqlTables);
    Cursor c = qb.query(db, sqlSelect, filter,null, null,
        null, null, null);

    c.moveToFirst();
    return c;

        }

答案 1 :(得分:1)

数据存储在Cursorposition的行位置onItemLongClick。使用moveToPosition()上的cursor方法,并检索值:

String one, two, three;

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        cursor.moveToPosition(position);
        one = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_1));
        two = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_2));
        // Etc...
    }
};

答案 2 :(得分:0)

public Cursor Retrive_SubCategory1(String id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("select "
                + "column_Name1 , "+"column_name2 " + " from "
                +"table_name " + " where "
                + "column_name " + "='" + id+ "'",
                null);
        return cursor;
    }