I am trying to set up a array adapter for android and need to convert my database out put to a array. Alternatively is there a way to use the Arraylist in the array adapter?
So I have an Array List of String arrays
This is the code for getting the results from the database and creating the 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] = "";
}
}
This is the code for converting the array list and using it in a array adapter for a list view
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);