Search code examples
androidmysqlsqlitelistviewcursor

getting column value from database onListitemClick


I am trying to get the value of one row from column "threadid" from database onListItemClick, but on every list item I click I get the value of previous row, and when I click the first item of the list my app crashes, I don't know what's the problem, Please help me catch my problem.My code is:

int threadid= datasource.getthreadid(id);
                  Toast.makeText(getActivity(),
                          String.valueOf(threadid), Toast.LENGTH_SHORT).show();

 public int getthreadid(long id)
  {
      int threadid = 0;
      String ide=String.valueOf(id);
      String queryz = "SELECT " + MySQLiteHelper.COLUMN_THREADID 
                + " FROM " + MySQLiteHelper.TABLE_NAME 
                + " WHERE " + MySQLiteHelper.COLUMN_ID + "=" + ide;
      Cursor cursor = database.rawQuery(queryz, null);
     // cursor.moveToFirst();
     // cursor.moveToPosition(Integer.parseInt(String.valueOf(id)));
    cursor.moveToFirst();
          threadid = cursor.getInt(cursor.getColumnIndex("threadid"));


      cursor.close();
    return threadid;      
  }

When my app crashes its logcat is:

09-13 15:58:57.465: V/Provider/Setting(7403): from settings cache , name = sound_effects_enabled value = 0
09-13 15:58:57.466: D/AndroidRuntime(7403): Shutting down VM
09-13 15:58:57.466: W/dalvikvm(7403): threadid=1: thread exiting with uncaught exception (group=0x41373908)
09-13 15:58:57.469: E/AndroidRuntime(7403): FATAL EXCEPTION: main
09-13 15:58:57.469: E/AndroidRuntime(7403): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:433)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at soft.b.peopleassist.DataSource.getthreadid(DataSource.java:41)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at soft.b.peopleassist.Receive.onListItemClick(Receive.java:136)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.widget.AbsListView.performItemClick(AbsListView.java:1114)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2946)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.widget.AbsListView$1.run(AbsListView.java:3722)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.os.Handler.handleCallback(Handler.java:615)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.os.Looper.loop(Looper.java:153)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at android.app.ActivityThread.main(ActivityThread.java:5000)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at java.lang.reflect.Method.invokeNative(Native Method)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at java.lang.reflect.Method.invoke(Method.java:511)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-13 15:58:57.469: E/AndroidRuntime(7403):     at dalvik.system.NativeStart.main(Native Method)

Solution

  • It would be probably better if you didn't rely on the position in the list being equal to the id from the database. Maybe you can store the database id in the object you use to populate the list and then get this object back in onClick and use its id.