简体   繁体   中英

Android - SQLite database to string

i have a "small" problem, i have SQLite databse and i need to get from a table "Champions" column "displayName" all values (text) to strings, and call them by id to display in textview? I cant find that already 2 hours... Any solution? (I need to get strings from one column, sort them by id. On load of Activity show that text value in textview, called by id.) Any good solution? Many thanks for every reply.

Sorry for bad english, and stupid question, but im new in programming (just java, im a little good in uselees Visual Basic).

EDIT: Or just eexport column to xml strings. Can I do that? Thanks

use 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;
    }

and cursor:

    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();

read this tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM