返回从一个SQLite数据库的所有数据到Android的两列 [英] Return all data from a SQLite DB into two columns in Android

查看:170
本文介绍了返回从一个SQLite数据库的所有数据到Android的两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的两列的数据库返回的所有数据到两个不同的 TextViews 。在一个尝试,我可以返回所有的数据,但它只能在一个的TextView ,在第二次尝试,我可以让它进入两个 TextViews ,但它只返回从数据库中的第一行。首先尝试:

I'm trying to return all data from my two column DB into two different TextViews. In one attempt, I can return all the data, but it's only in one TextView, in the second attempt, I can get it into the two TextViews, but it only returns the first row from the DB. First try:

    //--- Returns only first result

//--- DB Method
public String[] getPres2() {
    String[] columns = new String[] { KEY_PRES, KEY_YEAR};
    Cursor cursor = db.query(DB_TABLE, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        String pstYr[] = new String[2];
        pstYr[0] = cursor.getString(0);
        pstYr[1] = cursor.getString(1);
        cursor.close();
        this.db.close();
        return pstYr;
    }
    return null;

}

//--- in the activity
public void DBMethod2() {
    dba = new DBAdapter(this);
    dba.open();
    String[] pst_Str = dba.getPres2();
    dba.close();
    pst_TV.setText(pst_Str[0]);
    yrs_TV.setText(pst_Str[1]);

}

和第二次尝试:

    //--- Returns all results, but only in a single TextView
//--- DB Method
public String getPres3() {
    String[] columns = new String[] { KEY_PRES, KEY_YEAR};
    Cursor cursor = db.query(DB_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iPrs = cursor.getColumnIndex(KEY_PRES);
    int iYr = cursor.getColumnIndex(KEY_YEAR);

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
        result = result + cursor.getString(iPrs) + "\n" + cursor.getString(iYr) +   "\n" + "\n";
    }

    return result;

}


//--- In Activity
public void DBMethod3() {
    dba = new DBAdapter(this);
    dba.open();
    String pstY_Str = dba.getPres3();
    dba.close();
    pst_TV.setText(pstY_Str);

}

任何帮助吗?

推荐答案

您必须在同一时间通过游标进行迭代,一排,然后使用getString语法如下:

You have to iterate through the cursor, one row at a time, and then use the GetString syntax like this:

字符串MID = cursor.getString(cursor.getColumnIndex(COLUMN_ID));

String mId = cursor.getString(cursor.getColumnIndex(COLUMN_ID));

其中COLUMN_ID在SQLite数据库

where COLUMN_ID is the name of the column in the sqlite database

这篇关于返回从一个SQLite数据库的所有数据到Android的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆