0

スキーマは次のとおりです。

ここに画像の説明を入力

SQL クエリは次のとおりです。 SELECT * from unjdat where col_1 = " myWord ";

つまり、 col_1 がmyWordである行のすべての列を表示したいとします。

int i;
String temp;
words = new ArrayList<String>();
Cursor wordsCursor = database.rawQuery("select * from unjdat where col_1 = \"apple\" ", null); //myWord is "apple" here
    if (wordsCursor != null)
        wordsCursor.moveToFirst();
    if (!wordsCursor.isAfterLast()) {
        do {
            for (i = 0; i < 11; i++) {
                temp = wordsCursor.getString(i);
                words.add(temp);
           }
        } while (wordsCursor.moveToNext());
    }
    words.close();

問題はループにあると思います。forループを削除して a を実行するwordsCursor.getString(0)と、機能します。すべての列を取得するためにループする方法は?

:

  1. col_1 が null になることはありません。col_2 から col_11 のいずれかが、一部の行で null になる場合があります。
  2. テーブル内のすべての列とすべての行は一意です。
4

1 に答える 1

12

こうあるべき

Cursor cursor = db.rawQuery(selectQuery, null);
    ArrayList<HashMap<String, String>> maplist = new ArrayList<HashMap<String, String>>();
    // looping through all rows and adding to list

    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            for(int i=0; i<cursor.getColumnCount();i++)
            {
                map.put(cursor.getColumnName(i), cursor.getString(i));
            }

            maplist.add(map);
        } while (cursor.moveToNext());
    }
    db.close();
    // return contact list
    return maplist;

編集ユーザーは、ListView に HashMap を入力する方法を知りたがっていました

//listplaceholder is your layout
//"StoreName" is your column name for DB
//"item_title" is your elements from XML

ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.listplaceholder, new String[] { "StoreName",
                "City" }, new int[] { R.id.item_title, R.id.item_subtitle });
于 2013-06-23T08:38:01.740 に答える