Android 获取 Arraylist的问题从光标开始
我需要从光标获取一个值。该值需要采用这种格式:
List<double[]> values = new ArrayList<double[]>();
问题是当我执行“.add”时,它会错误地要求我将其更改为“length”。我必须将它放在列表中才能使应用程序正常工作。这是我的代码,有办法做到这一点吗?我做错了什么?
cursor = managedQuery(Provider.CONTENT_URI,
proj,
null,
null,
null);
while (cursor.moveToNext()) {
double value;
column_value = cursor.getColumnIndex(CsvProvider.VALUE);
value = cursor.getDouble(column_value);
values.add(value); //this did not work
values.add(new double[] {value}); //this did work
} ;
谢谢迈克!
I need to get a value from my cursor. The value needs to be in this format:
List<double[]> values = new ArrayList<double[]>();
The problem is when I do the ".add" it errors wanting me to change it to "length". I have to have it in the List for the app to work at all. Here is my code, is there a way to do that? What am I doing wrong?
cursor = managedQuery(Provider.CONTENT_URI,
proj,
null,
null,
null);
while (cursor.moveToNext()) {
double value;
column_value = cursor.getColumnIndex(CsvProvider.VALUE);
value = cursor.getDouble(column_value);
values.add(value); //this did not work
values.add(new double[] {value}); //this did work
} ;
Thanks Mike!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,你永远不应该解释错误。如果出现错误,请将其复制并粘贴到您的问题中。
至于您的问题:
您有一个双数组列表,即 cValues 列表中的每个条目都需要是 double[]。然而,您只是添加一个普通的双精度值。那是行不通的。我不知道你的列表应该如何设置,如果你错误地将 [] 放入或者你确实需要一个数组列表,但假设每个条目都需要一个双精度数组,那么你需要添加
new double[] { value }
。旁注:为什么不简单地
while (cursor.moveToNext()) { ... }
而不是这个 do/while 结构呢?First of all, you should never paraphrase errors. If you get an error, copy and paste it into your question.
As to your problem:
You have a list of double arrays, i.e. every single entry in your cValues List needs to be a double[]. Instead, however, you're just adding a plain double. That won't work. I don't know how your list is supposed to be setup, if you put the [] in by mistake or if you really need a list of arrays, but assuming that every entry needs an array of one double, then you need to add a
new double[] { value }
.Side note: Why not simply
while (cursor.moveToNext()) { ... }
instead of this do/while construct?