我想爲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