2013-03-06 34 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.之后,将该数组值存储在列表中。