使用数据库列填充微调器,第一行被丢弃
db.open();
cursor = db.getAllRecords();
int i = 0;
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
spinnerItems[i] = cursor.getString(NAME_COLUMN);
Log.v(TAG, "spinnerItem "+i+"="+spinnerItems[i]);
i++;
}
为什么上面的代码在 LogCat 中给出这个结果?:
spinnerItem 0=name1
spinnerItem 1=name2
spinnerItem 2=name3
spinnerItem 3=name4 and so on
当数据库看起来像这样时:
row name
0 name0
1 name1
2 name2
3 name3
4 name4 and so on.
用数据库一列的全部内容填充微调器的最佳方法是什么?上面我(尝试)创建 spinnerItems[] ,然后将 arrayAadapters 放入微调器中。
db.open();
cursor = db.getAllRecords();
int i = 0;
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
spinnerItems[i] = cursor.getString(NAME_COLUMN);
Log.v(TAG, "spinnerItem "+i+"="+spinnerItems[i]);
i++;
}
Why does the code above give this result in LogCat?:
spinnerItem 0=name1
spinnerItem 1=name2
spinnerItem 2=name3
spinnerItem 3=name4 and so on
When the database looks like this:
row name
0 name0
1 name1
2 name2
3 name3
4 name4 and so on.
And what is the best way to populate a spinner with the entire content from one column of a database? Above I (try to) create spinnerItems[] whish I then arrayAadapters into the spinner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的主要问题,一切都关闭的原因在于您的 for 循环逻辑。
循环从 moveToFirst() 开始, - 光标现在位于第 0 行。
然后,循环检查它是否通过条件,即调用 moveToNext() - 光标现在位于第 1 行。
因此,第一次迭代时,您的光标位于第 1 行(或者如果您的光标位于第 1 行,它将完全绕过循环)只有 1 行!)。
更好的 for 循环:
关于您的其他问题,不幸的是我没有答案......我也总是像这样以编程方式构建它们。不过,快速的谷歌搜索为我提供了这一点:
http://androidforbeginners.blogspot.com/2010 /02/how-to-populate-spinner-widget-from.html
For you main question, the reason everything is off is in your for loop logic.
The loop starts with moveToFirst(), - cursor now on row 0.
Then, the loop checks out if it passes the conditional, which is a call to moveToNext() - cursor now on row 1.
So the very first iteration, your cursor is on row 1 (or it will bypass the loop entirely if your cursor only has 1 row!).
A better for loop:
In regards to your other question, unfortunately I have no answer... I always build them programmaticaly like that as well. A quick google search provided me with this, though:
http://androidforbeginners.blogspot.com/2010/02/how-to-populate-spinner-widget-from.html