我想为android设置一个数组适配器,并且需要将我的数据库转换为放入数组。或者,有没有办法在阵列适配器中使用Arraylist?如何将Arraylist <String[]>转换为String []
所以我有字符串数组的数组列表
这是为从数据库中获取的结果,并创建该ArrayList
public ArrayList<String[]> fetchUser(String name) throws SQLException {
ArrayList<String[]> myArray = new ArrayList<String[]>();
int pointer = 0;
Cursor mCursor = mDb.query(TABLE_NAME, new String[] {"_id", "name",
"tel", "mob"}, "name LIKE '%" + name + "%'", null,
null, null, null);
int firstNameColumn = mCursor.getColumnIndex("name");
int telColumn = mCursor.getColumnIndex("tel");
if (mCursor != null){
if (mCursor.moveToFirst()){
do {
myArray.add(new String[3]);
myArray.get(pointer)[0] = mCursor.getString(firstNameColumn);
myArray.get(pointer)[1] = mCursor.getString(telColumn);
pointer++;
} while (mCursor.moveToNext()); // If possible move to the next record
} else {
//if no records are returned then add a new array and say no results.
myArray.add(new String[3]);
myArray.get(pointer)[0] = "NO RESULTS";
myArray.get(pointer)[1] = "";
}
}
代码这是用于将数组列表和使用该代码它在用于列表视图
ArrayList<String[]> searchResult = new ArrayList<String[]>();
searchResult = dbh.fetchUser("Ross Mackie");
String[] array = searchResult.toArray(new String[searchResult.size()]);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
lv.setAdapter(arrayAdapter);
将'ArrayList'转换为'String []'不会使敏感 –
Selvin
2015-03-31 14:54:33
@Selvin那么我该如何去使用Array适配器中的ArrayList? –
RapGodRory
2015-03-31 14:57:18
然后你必须使用ArrayAdpater ......但显然你不应该使用String [],而是使用一些其他容器(自己的POJO)或SimpleAdapter与HashMap **编辑:**或甚至更好CursorAdapter ...作为数据来自sqlite ...不,我不会提供示例,因为在互联网上有大量的例子 –
Selvin
2015-03-31 14:57:55