Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:从游标创建行ID数组-但如何创建?_Android_Arrays_Sqlite_Cursor - Fatal编程技术网

Android:从游标创建行ID数组-但如何创建?

Android:从游标创建行ID数组-但如何创建?,android,arrays,sqlite,cursor,Android,Arrays,Sqlite,Cursor,我有一个db集合(SQLite),我想创建一个行ID数组。我认为以下措施应该有效: db.open(); String Chapter = "Something"; Cursor c = db.getSetsByChapter(Chapter); int[] ids = {}; if (c.moveToFirst()){ int i = 0; do { ids[i] = Integer.parseInt(c.getString(0)); i++; } while (c.

我有一个db集合(SQLite),我想创建一个行ID数组。我认为以下措施应该有效:

db.open();
String Chapter = "Something";
Cursor c = db.getSetsByChapter(Chapter);
int[] ids = {};
if (c.moveToFirst()){
    int i = 0;     
 do {
 ids[i] = Integer.parseInt(c.getString(0));
 i++;
 } while (c.moveToNext());
}


db.close();
Toast.makeText(this,"The array contains " + ids.length + " elements",Toast.LENGTH_LONG).show();
但我一直得到: 无法启动活动组件。。。java.lang.ArrayIndexOutOfBoundsException


我对java和android编码非常陌生。我已经做了三天了,但还是失去了它

首先,您需要向我们显示发生异常的行

第二,
int[]ids={}声明长度为0的整数数组。然后你试着

 ids[i] = Integer.parseInt(c.getString(0));

将数据放入索引0。这是不可能的,因此会出现数组索引越界异常。

您需要分配数组。使用
列表
,然后将列表转换为整数数组

,因此在声明数组之前,我需要计算集合中有多少个元素。正在研究如何做到这一点。//--从条目中获取ID--db.open();String Chapter=“某物”;光标c=db.getSetsByChapter(章节);int[]ids=newint[c.getCount()];如果(c.moveToFirst()){int i=0;do{ids[i]=Integer.parseInt(c.getString(0));i++;}而(c.moveToNext());}db.close();Toast.makeText(这个“数组包含”+ids.length+“元素”,Toast.length_LONG.show();}你不能这样做。您不能创建基本体列表。它必须是列表<代码>列表ID=新建ArrayList()