数组方法返回数组,只有第一个值?
我正在尝试从 sqlLite DB 获取列数据并将该数据放入数组方法中,然后调用该方法并显示数据,由于某种原因我只获取列中的第一个变量,请帮忙!
-是的,该列中有更多变量
public String[] getScore01() {
String[] column1 = new String[] { KEY_NAME };
Cursor c = ourDatabase.query(DATABASE_TABLE, column1, null, null, null,
null, null);
String[] result = {};
int iName = c.getColumnIndex(KEY_NAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String[] result1 = { "" + c.getString(iName) };
result = result1.clone();
}
return result;
}
,在这里我们调用该方法并将数据设置为数组...
Database info = new Database(this);
info.open();
String[] data = info.getScore01().clone();
info.close();
将文本视图 tvVVTEST 设置为 data[0] 有效,但将其设置为 data[1] 或其他任何内容都会失败为什么???
tvVVTEST.setText("" + data[0]); //works
//tvVVTEST.setText("" + data[1]); // Does Not Work
I am trying to get column data from sqlLite DB and put that data into an array method and then call the method and display the data, for some reason I am only getting the first variable in my column, help please!!!!
-Yes there are more variables in that column
public String[] getScore01() {
String[] column1 = new String[] { KEY_NAME };
Cursor c = ourDatabase.query(DATABASE_TABLE, column1, null, null, null,
null, null);
String[] result = {};
int iName = c.getColumnIndex(KEY_NAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String[] result1 = { "" + c.getString(iName) };
result = result1.clone();
}
return result;
}
and here we call the method and get the data set it to an array...
Database info = new Database(this);
info.open();
String[] data = info.getScore01().clone();
info.close();
setting the textview tvVVTEST to data[0] works, but setting it to data[1] or anything else fails why???
tvVVTEST.setText("" + data[0]); //works
//tvVVTEST.setText("" + data[1]); // Does Not Work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看一下这个循环:
您只将
result1
设置为单个元素数组,然后将result
设置为该数组的克隆。因此result
将仅引用单元素数组。我建议您使用List
代替:(更改您的方法以声明它也返回
List
。)然后您应该能够使用
顺便说一句,您似乎在任何需要字符串的地方都使用
"" + x
。即使x
不是 字符串,这也是一个坏主意 - 使用String.valueOf(x)
代替 - 但当x
不是字符串时,情况会更糟>x 是一个字符串。您也不需要到处克隆数组(或列表)。哦,您应该将
info.close()
方法放在finally 块中,这样即使抛出异常也可以关闭数据库。Look at this loop:
You're only ever setting
result1
to a single element array, and thenresult
to a clone of that array. Thereforeresult
will only ever refer to a single-element array. I suggest you use aList<String>
instead:(Change your method to declare that it returns a
List<String>
as well.)You should then be able to use
By the way, you seem to be using
"" + x
wherever you want a string. That's a bad idea even whenx
isn't a string - useString.valueOf(x)
instead - but it's even worse whenx
is a string.You also don't need to be cloning arrays (or lists) all over the place. Oh, and you should put the
info.close()
method in a finally block, so that you close the database even if an exception is thrown.没必要这样打电话。将其更改为
并且您已声明结果两次。第二,确保数组值每次都会插入到新位置。否则它将覆盖,您将只得到一个值
已编辑
内部循环更改为
No need to call like this. Change this to
And you have declared result two times.Second make sure that array value will insert on new position everytime.Otherwise it will over write and you will get only one value
Edited
changes inside loop to
添加以下内容:
如果是一个,那么您的数组中可能只有一个元素。
Add the following:
If its one then you probably only have one element in your array.
如果你可以使用 for() 语法,这对我来说是新闻。如果使用 while 循环会发生什么?
if you can use that for() syntax that news to me. What happens if you use a while loop instead?