数据从 sqlite 到 ArrayList<String>; [英] data from sqlite into ArrayList<String>
问题描述
我正在使用代码这个博客 有一个可拖动的列表.本教程使用自定义 DragNDropAdapter将内容作为 ArrayList.
i am using code this blog to have a draggable list. This tutorial is using a custom DragNDropAdapter that takes the content as an ArrayList.
在我的 listActivity 中,我查询了一个返回列 name
的表.它插入了 11 个值.我尝试使用多种方法将其从 String[] 转换为 ArrayList,例如:
In my listActivity i query a table with returned column name
.It has 11 values inserted.
i tried to convert it to ArrayList from String[] with many ways such as :
String[] from = new String[]{DbManager.KEY_NAME};
ArrayList<String> content = new ArrayList<String>();
for (int i=-1,l=from.length; ++i<l;) {
content.add(from[i]);
//Log.i("ArrayList", from[i]);
}
或
while(!mShopCatCursor.isAfterLast()){
content.add(mShopCatCursor.getString(0));
}
我得到的是一个只有列名称的列表,name
.你有什么想法吗
what i get is a list with just the name of the column, name
.
do you have any ideas
推荐答案
String[] from = new String[]{DbManager.KEY_NAME};
因为您的字符串数组只有一个值,即 KEY_NAME.
Because your string array has only one value which is KEY_NAME.
你需要做的是,
使用循环从 Cursor 获取值并将其填充到上面的 String[] 中.
Get values from Cursor using loop and populate it String[] above.
Cursor userCur = adaptor.getYourData();
if (userCur != null) {
String[] strArr = new String[userCur.getCount()];
startManagingCursor(userCur);
if (userCur.moveToFirst()) {
int count = 0;
do {
String userName = userCur.getString(1);
strArr[count] = userName.trim();
count++;
} while (userCur.moveToNext());
}
ArrayList<String> content = new ArrayList<String>();
for (int i=-1,l=from.length; ++i<l;) {
content.add(from[i]);
//Log.i("ArrayList", from[i]);
}
}
注意:我没有在 IDE 中验证过,可能存在语法错误.
Note: I haven't validated this in IDE, there may be syntax errors.
这篇关于数据从 sqlite 到 ArrayList<String>;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!