1

我只是从 sqlite 表中显示一个列表。我没有使用任何 BDHelper 类。仅使用此代码,我如何获得 id。

在此处输入图像描述

当我单击第一项时,它显示 0,如表中所示,它的 id 为 1。下面是我的代码。

SQLiteDatabase myDB;
        try {
            myDB = openOrCreateDatabase(DB_NAME,
                    SQLiteDatabase.CREATE_IF_NECESSARY, null);          
            myDB.execSQL("create table if not exists " + COUNTRY_TABLE
            + "(country_id INTEGER PRIMARY KEY,"
            + "country_title text," 
            + "country_status int(11));");          

             /*myDB.execSQL("insert into " + COUNTRY_TABLE +" values "+
             "(null,'India',1)," +
             "(null,'China',1)," +
             "(null,'Australia',1)," +
             "(null,'Japan',1)," +
             "(null,'Germany',1)");*/


            Cursor c = myDB.rawQuery("select country_id,country_title from "
                    + COUNTRY_TABLE + " where country_status=1", null);
            if (c != null) {
                if (c.moveToFirst()) {
                    do {
                        int id = c.getInt(c.getColumnIndex("country_id"));
                        String name = c.getString(c.getColumnIndex("country_title"));                       
                        clist.add(id + ") " + name);
                    } while (c.moveToNext());
                    //int itemcount = clist.size();
                    //Toast.makeText(MainActivity.this, itemcount, Toast.LENGTH_LONG).show();
                    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, clist));
                }
            }

        } catch (SQLiteException se) {
            Toast.makeText(MainActivity.this, se.getMessage().toString(),Toast.LENGTH_LONG).show();
        }

protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Toast.makeText(MainActivity.this,String.valueOf(id),Toast.LENGTH_LONG).show();
    }

请建议我应该怎么做才能获得表格而不是职位。

4

1 回答 1

0

只需将您的查询更改为按 ID 搜索:

Cursor c = myDB.rawQuery("select country_id,country_title from "
                + COUNTRY_TABLE + " where country_id=" + id, null);
于 2013-10-07T13:45:09.660 回答