我有一个“小”问题,我有SQLite数据库,我需要从表“冠军”列“ displayName”中获取所有值(文本)为字符串,并通过ID调用它们以在textview中显示?我已经找不到2个小时了吗? (我需要从一列中获取字符串,按ID对其进行排序。在Activity的负载下,显示textview中的文本值由ID调用。)有什么好的解决方法?非常感谢您的每一个答复。

对不起,英语不好,还有愚蠢的问题,但是我在编程方面是个新手(只是Java,我在Visual Basic中有点用)。

编辑:或只是eexport列到xml字符串。我可以那样做吗?谢谢

最佳答案

使用DataBaseHelper:

    DataBaseHelper myDbHelper = new DataBaseHelper(this.context, "animals.db");
    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        Toast.makeText(context,
                context.getString(R.string.error), Toast.LENGTH_SHORT).show();
        throw new Error("Unable to create database");
    }
    try {
        myDbHelper.openDataBase();
    } catch (SQLException sqle) {
        Toast.makeText(context,
                context.getString(R.string.error), Toast.LENGTH_SHORT).show();
        throw sqle;
    }


和光标:

    SQLiteDatabase db = myDbHelper.getWritableDatabase();

    Cursor c = db.query("animals", null, null, null, null, null, null);

    ArrayList<Animals> animals = new ArrayList<Animals>();

    int idColIndex = c.getColumnIndex("_id");
    int nameColIndex = c.getColumnIndex("name");
    ArrayList<String> nameList = new ArrayList<String>();
    if (c.moveToFirst()) {
        do {
            String name = c.getString(nameColIndex);
            nameList.add(name);
        } while (c.moveToNext());
    }
    c.close();
    db.close();


阅读this tutorial

关于java - Android-SQLite数据库到字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25791543/

10-12 00:38
查看更多