简体   繁体   中英

Android SQLite column value does not exists issue

need help in some wired problem. I have a sqlite table items as following

item_id    item_name
1          first
2          second
3          third

and so on

this piece of code is inserting item_id and item_name to arrays perfectly fine. But when i assign it to CustomCursorAdapter it through exception

"column 'first' does not exists"

please help I am newbie to Android.

        c = db.rawQuery("select item_id as _id, item_name from items", null);
        int i = 0;
        c.moveToFirst();
        do {
            from[i] = c.getString(1);
            to[i] = c.getInt(0);
            Log.i("item ", to[i] + " " + from[i]);
            i++;
        } while (c.moveToNext());
        try {
            CustomCursorAdapter ca = new CustomCursorAdapter(this,
                    android.R.layout.simple_list_item_1, c, from, to);

            getListView().setAdapter(ca);
        } catch (Exception e) {
            Log.e("Ex ", e.getMessage()); // exception : column 'first' does not exists.
        }

This is because you are setting from[0] = c.getString(1) .

getString() is zero-based so in your case is returning "first" . Then you are passing from as the from parameter of a class (that I guess, extends SimpleCursorAdapter) and then you are receiving this error when the adapter is trying to map this column name. In this case from represents the list of column names holding the data to bind to the UI, which is represented by to .

in from parameter in

CustomCursorAdapter ca = new CustomCursorAdapter(this,
                    android.R.layout.simple_list_item_1, c, from, to);

needed column names as arguments, whereas you are passing column values array to from array, whereas 'to' needed to be id of textviews of layouts these columns to de projected, so in this example id is android.R.id.textview1, so do following:

String[] from= new String[]{"item_name"};
int[] to= new int[]{android.R.id.textView1};

and then execute your query on these parameters.

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