1

我是安卓新手。我正在尝试编写一个方法,该方法将在 3 个不同的文本视图中显示来自数据库同一行的 3 个不同列。现在,我有 3 个相同的方法,每个方法显示同一行的不同列:

        Display part of Method #1:
if (cursor != null){
        cursor.moveToFirst();
        result = result
        + cursor.getString(0);
        return result;
    }
    return null;


    Display part of Method #2:
if (cursor != null){
        cursor.moveToFirst();
        result = result
        + cursor.getString(1);
        return result;
    }
    return null;

    Display part of Method #3:
if (cursor != null){
        cursor.moveToFirst();
        result = result
        + cursor.getString(2);
        return result;
    }
    return null;

所以我的问题是:有没有办法编写一个方法,其结果类似于“对于单个 DB 行,cursor.getString(0);在 TextView #1 中显示,cursor.getString(1);在TextView #2, cursor.getString(2); 在 TextView #3 中显示?”

4

2 回答 2

1
if (cursor != null){
    cursor.moveToFirst();
    String string1 = cursor.getString(0);
    .... string2 = cursor.getString(1);
    .... string3 = cursor.getString(2);
    textView1.setText(string1);
    textView2.setText(string2);
    textView3.setText(string3);
}

对于数据库适配器,您可以做一件事

if (cursor != null){
    cursor.moveToFirst();
    String strings[] = new String[3];
    strings[0] = cursor.getString(0);
    strings[1] = cursor.getString(1);
    strings[2] = cursor.getString(2);
    return strings;

在调用活动中,您可以轻松地将这些返回的数组元素设置为文本视图。

希望这会帮助你。

于 2012-04-10T15:01:16.063 回答
0

在 TextView 中使用字符串生成器和“\n”字符。或者只有 3 个 TextView。

于 2012-04-10T15:00:48.210 回答