2013-03-06 79 views
0
public List<String> getDays(long iid) 
{ 
    List<String> days= new ArrayList<String>(); 
    cursor = database.query(MySqliteHelper.TABLE_REPEAT, daysColumn, MySqliteHelper.COLUMN_TID + "=" + iid, null, null, null, null); 
    while(fg>=1) 
    { 
     cursor = database.query(MySqliteHelper.TABLE_REPEAT, daysColumn, MySqliteHelper.COLUMN_TID + "=" + iid, null, null, null, null); 
      String day = cursor.getString(cursor.getColumnIndex(MySqliteHelper.COLUMN_DAYS)); 
     days.add(day); 
     cursor.moveToNext(); 
      if(cursor.isAfterLast()) 
      { 
     fg=0; 
      } 
    } 
    cursor.close(); 
    fg=1; 
    return days; 
    } 

現在我得到arrayindexoutofbound異常!我是新來的Android和編程.. 建議我使用什麼技術。從表中取多個值

和我的日誌貓顯示: 11月3日至6日:20:12.941:E/AndroidRuntime(2025):致命異常:主要 11月3日至6日:20:12.941:E/AndroidRuntime(2025):JAVA。 lang.RuntimeException:無法啓動活動ComponentInfo {com.example.habitator/com.example.habitator.AlarmDays}:android.database.CursorIndexOutOfBoundsException:請求索引-1,大小爲1

+0

親愛的,你確切地知道'光標'是什麼?以上代碼你在做什麼? – 2013-03-06 10:31:49

+0

我編輯了我的問題。請檢查 – AnRu 2013-03-06 10:52:34

回答

0

爲什麼您再次查詢數據庫?如果cursor.isAfterLast()滿足,則使用break。因爲這個,你得到錯誤arrayindexoutofbound和

也指定cursor.moveToFirst();在第一位置移動光標

試試這個:

public List<String> getDays(long iid) 
{ 
    List<String> days= new ArrayList<String>(); 
    cursor = database.query(MySqliteHelper.TABLE_REPEAT, daysColumn, MySqliteHelper.COLUMN_TID + "=" + iid, null, null, null, null); 
    cursor.moveToFirst(); 
    while(fg>=1) 
    { 
     if(cursor.isAfterLast()) 
      { 
       fg=0; 
       break; //brek the loop 
      } 
     String day = cursor.getString(cursor.getColumnIndex(MySqliteHelper.COLUMN_DAYS)); 
     days.add(day); 
     cursor.moveToNext(); 
    } 
    cursor.close(); 
    fg=1; 
    return days; 
    } 

希望這會爲你工作!

+0

我編輯了我的代碼。當你得到-1索引,所有的杉木,檢查你看是否cursor.isAfterLast()如果是這樣,打破循環,如果沒有,那麼只執行下一個代碼 – 2013-03-06 11:28:54

+0

我的程序不會現在崩潰,但只顯示一個行。其實有鬃排與約束!我真的不知道爲什麼它不能添加! – AnRu 2013-03-06 11:34:52

+0

你是否從while循環中刪除了database.query()代碼? 你已經寫了兩次,它再次初始化相同的光標。 無需將它寫入while循環。 請看代碼以供參考,並讓我知道它的工作與否 – 2013-03-06 11:37:32

0
 Cursor c=db.rawQuery("select * from table_name where user_fk='" + get_userid + "'",null); 
    List<String[]> list = new ArrayList<String[]>(); 

     c.moveToFirst(); 
     while(!c.isAfterLast()) 
     { 
      title=new String[]{c.getString(3),c.getString(0)}; 
      list.add(title); 
      c.moveToNext(); 
     }  

    c.close(); 
    db.close(); 

1.使用數組從表中獲取多個值。

2.之後,將該數組值存儲在列表中。