1

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);
RapGodRory
  • 774
  • 5
  • 8
  • converting `ArrayList` to `String[]` doesn't make sens – Selvin Mar 31 '15 at 14:54
  • @Selvin So how would I go about using the ArrayList in the Array adapter? – RapGodRory Mar 31 '15 at 14:57
  • then you have to use ArrayAdpater ... but obviously you shouldn't use String[] at all but some other container (own POJO rather) or SimpleAdapter with HashMap **edit:** or even better CursorAdapter ...as data come from sqlite ... and no i will not provide example as there is a plenty of examples in the internet – Selvin Mar 31 '15 at 14:57

2 Answers2

0
List<String> list = ..;
String[] array = list.toArray(new String[list.size()]);

For example:

List<String> list =new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[list.size()]);

The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, and returned. You can pass an empty array as well, but you can also pass an array with the desired size.

Original answer: Converting 'ArrayList<String> to 'String[]' in Java

Community
  • 1
  • 1
smail2133
  • 936
  • 2
  • 7
  • 24
0
 List<String> list = new ArrayList<String>();

 ArrayAdapter<String> oneAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);

 oneAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 theSpinner.setAdapter(oneAdapter);

Convert list into an adapter and add it to a spinner, not exactly what you were looking for, but its something.

worker
  • 33
  • 3
  • 13