在Stackoverflow上关注此question的教程。查询数据库后,我在变量results
中有记录。如何格式化结果以使其可以是数组。以下是一个例子:
lvActivityCategory.setAdapter(new categoryCursorAdaptor(this, new String[] { "data1", "data2" }));
这是我目前使用的代码:
List<String> arrayCatNames = new ArrayList<String>();
String query = "SELECT * FROM category ORDER BY name ASC";
Cursor results = myDB.rawQuery(query, null);
while(results.moveToNext()){
String catName = results.getString(results.getColumnIndex("name"));
arrayCatNames.add(catName);
}
android.text.TextUtils.join(",", arrayCatNames);
lvActivityCategory = (ListView) findViewById(R.id.lvActivityCategory);
lvActivityCategory.setAdapter(new categoryCursorAdaptor(this, new String[] { arrayCatNames })); //arrayCatNames should be e.g. "item 1", "item 2", "item 3"
答案 0 :(得分:1)
好的,在你的while循环之后执行以下操作
String[] catNamesArr = new String[arrayCatNames.size()];
catNamesArr = arrayCatNames.toArray(catNamesArr);
您可以阅读以下问题了解更多信息:Convert ArrayList<String> to String[] array