Search code examples
androidkotlinandroid-sqliteandroid-cursor

How to extract row values from a cursor object?


I used this query "SELECT English FROM Sheet1 WHERE Category_id='WL_1'". here it is returning 13 rows and 1 column but i am not getting how to extract those 13 rows values from cursor. I am trying:

 for(i in 0 until testData!!.count){
     Log.e("HomeActivity", "Fetched column data:"+testData!!.getString(testData!!.getColumnIndex("English")))
   
      testData!!.moveToNext()
}

But it is running only 3 times and returning row 0 item only not row 1,2, and so on. So please tell me what I am doing wrong here.


Solution

  • you can try this

            String sql="SELECT English  FROM Sheet1 WHERE Category_id='"+WL_1+"' ";
            Cursor cr=db.query(sql);
    
            ArrayList arrayList=new ArrayList();
            while (cr.moveToNext()) {
                try {
                    String English = cr.getString(cr.getColumnIndex("English"));
    
                    arrayList.add(English);
    
                }catch (Exception e){
                    Log.d(activity," Something went wrong");
                }
            }