0

我正在尝试从数据库 onListItemClick 的“threadid”列中获取一行的值,但是在我单击的每个列表项上,我都会获取上一行的值,当我单击列表的第一项时,我的应用程序崩溃了,我不'不知道是什么问题,请帮我解决我的问题。我的代码是:

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

当我的应用程序崩溃时,它的 logcat 是:

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)
4

1 回答 1

0

如果您不依赖列表中的位置等于数据库中的 id,那可能会更好。也许您可以将数据库 ID 存储在用于填充列表的对象中,然后在 onClick 中获取该对象并使用其 ID。

于 2013-09-13T12:30:03.857 回答