【问题标题】:Android : How to create Cursor object to get item id in SQLite?Android:如何创建 Cursor 对象以在 SQLite 中获取项目 ID?
【发布时间】:2015-02-09 02:08:18
【问题描述】:

如何创建游标对象以从数据库中获取项目 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));
}

【问题讨论】:

    标签: android sqlite cursor


    【解决方案1】:

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

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

    您还可以使用: cursor.getColumnIndex(COLUMN_NAME) 而不是 cursor.getString(0)

    您的代码似乎是正确的,我只是检查一下我提到的以下内容。

    【讨论】:

    • 如何在这里传递id 值?我知道您希望我传递位置,但该值位置是列表视图中项目的位置,它与数据库行 ID 不同。这就是我卡住的地方。请检查我的这个问题stackoverflow.com/questions/28393156/…
    • setOnItemClickListener,您可以使用:myListView.getItemAtPosition(position); 这样我认为您的列表视图中的对象中会有 ID,对吧?
    • Apurva,你成功了吗?
    • 是的,我从这里得到了解决方案stackoverflow.com/questions/28393156/…
    • 很好,选择此线程上的任何回复作为答案,然后您的问题就有答案了;)
    【解决方案2】:
    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;
    

    【讨论】:

    • 我知道你想让我传递位置,但该值位置是列表视图中项目的位置,与数据库行 ID 不同。这就是我卡住的地方。请检查我的这个问题stackoverflow.com/questions/28393156/…
    【解决方案3】:

    这是一个示例代码,希望你能从中得到帮助

       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"));
    
    
            }
    
        });
    
    }
    

    }

    【讨论】:

      【解决方案4】:

      试试这个:

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

      【讨论】:

      • 我没有尝试过这段代码,但我确信这段代码会尝试将列表视图项位置与数据库行 ID 匹配。在我的情况下,它并不总是相同的,例如对于相同的项目列表视图项目位置可能是 3 并且数据库行 id 大于该数字
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 2014-04-08
      • 2022-01-19
      相关资源
      最近更新 更多