It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
我在数据库适配器类中运行查询,如下所示:在其中提供ID并想获取与该ID关联的行:

    public Cursor fetchRow(String a) {
    String Key = a;

    return mDb.query(DATABASE_TABLE0, new String[] {IDno1, GIDno1,Name1,Owner1,Breed1,Gender1}, "IDno=?",new String[]{Key}, null, null, null, null);
}


现在在我的班级中,我想将从该查询返回的各个元素绑定到不同的变量,但是我对如何实现这一点感到困惑。我正在做类似的事情:

    private void fillData() {
    Cursor c = DBHelper.fetchRow();

   // List<String> idList = new ArrayList<String>();
    if (c.moveToFirst()) {


             }
}

最佳答案

try {
            c = null;
            String select = "Select * FROM tbl";
            c = db.rawQuery(select, null);
            if (null!=c) {
                while (c.moveToNext()) {
                    //Here you can directly set the value in textview
                    txtview.setText(c.getString(c.getColumnIndex("column_name")));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            c.close();
        }


这样,您可以获取您的值并将其直接设置为textview。如果您确定只有一行,那么不要使用while语句。

if (null!=c) {
    c.moveToNext();
    txtview.setText(c.getString(c.getColumnIndex("column_name")));
}


希望这会有所帮助。

关于android - 从SQLite DB中提取一行并将值保存到单个变量中,并将其显示在不同的文本 View 中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12796330/

10-12 01:45