Android:如何创建Cursor对象以获取SQLite中的项ID?

时间:2015-02-08 17:03:24

标签: android sqlite cursor

如何创建游标对象以从数据库中获取项目ID?

这是我的DBHelper方法,请参阅Cursor方法

public int getItemIdByPosition(int position) {

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
    cursor.moveToPosition(position);
    return Integer.parseInt(cursor.getString(0));
}

4 个答案:

答案 0 :(得分:2)

似乎是正确的。 也许通过方法传递的位置不正确,也许你使用的效率更高,而不是在你的方法上传递一个位置传递ID:

"select * from " + TABLE_NAME + " where id = " = id

您还可以使用: cursor.getColumnIndex(COLUMN_NAME)代替cursor.getString(0)

你的代码似乎是正确的,我只是检查下面提到的事情。

答案 1 :(得分:1)

这是一个示例代码,希望您能从这个

获得帮助
   private void displayListView(String getter){
    //get the customer data from the db and feed them to cursor and load the   data to lest 
    Cursor cursor = dbHelper.fetchallcustomercompany(getter);

    String[] columns = new String[] {
            //get the needed columns of the db and feed them in to string array

        DBCreater.Key_customer_Shop
    };

    int[] to = new int[]{
            //get the textboxs in xml layout,which going to display the values in to integer array  
            R.id.tv_demo_search_text_Isuru
    };
    //address the xml list view to java
    final ListView listView = (ListView)findViewById(R.id.lv_searchcustomer_cuzlist_Isuru);
    // feed the context,displaying layout,data of db, data source of the data and distination of the data
    if(cursor.getCount()==0){
        Toast.makeText(getApplicationContext(), " No matching data", Toast.LENGTH_SHORT).show();
    }
    else{
     dataAdapter = new SimpleCursorAdapter(this, R.layout.demo_search, cursor, columns, to,0);
     //load the data to list view
    listView.setAdapter(dataAdapter);
    //what happen on when user click on the item of the list view

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Cursor cursor =(Cursor)listView.getItemAtPosition(arg2);
            //get the value of the customer name from the clicked listitem
            String name=cursor.getString(cursor.getColumnIndexOrThrow("customer_shop"));


        }

    });

}

}

答案 2 :(得分:1)

String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + POSITION + " = " + position;

    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    int userId;

    if (cursor.moveToFirst())
    {
        userId = cursor.getInt(0);  
    }

    cursor.close();
    return userId;

答案 3 :(得分:0)

试试这个:

    public int getItemIdByPosition(int position) {

    int itemID = 0;
    Cursor localCursor = database.rawQuery("select * from " + TABLE_NAME,
            null);
    int i = localCursor.getColumnIndex("ID");
    if (localCursor.moveToFirst()) {

        do {

            itemID = Integer.parseInt(localCursor.getString(i));
        } while (localCursor.moveToPosition(position));
    }
    localCursor.close();
    return itemID;
}