0

I am currently using a simpleCursorAdaptor and a cursor to load a spinner with data, however I would prefer to convert the cursor to a simple array and use this instead (as the list is short and static).

What's the simplest way of doing this?

My code currently is:

    private void loadEmployeeList(){
    LoginDataHandler dataHandler = new LoginDataHandler(getContentResolver());
    Cursor data = dataHandler.activeEmployeeList();
    if (null!=data){

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_spinner_item, 
                data, 
                new String[]{MyobiliseData.Columns_employees.NAME},
                new int[] { android.R.id.text1 }
                );

        // Attach the data to the spinner using an adaptor
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

    }

}
WaterBoy
  • 697
  • 3
  • 13
  • 35
  • Why you want to convert cursor data to array? Android already provide a feasible way to manage cursor data using CursorAdapter. – user370305 Jun 19 '12 at 05:51
  • CursorAdapter is best way to manage cursor data with its id along it Spinner or Listview. – user370305 Jun 19 '12 at 05:58
  • thanks - i think you are probably right - I just thought it seemed like a larg-ish overhead for such a small list – WaterBoy Jun 19 '12 at 06:24

4 Answers4

1
ArrayList<String> mArrayList = new ArrayList<String>();
for(data.moveToFirst(); !data.isAfterLast(); data.moveToNext()) {
    mArrayList.add(data.getString(data.getColumnIndex(MyobiliseData.Columns_employees.NAME)));
}

now you can proceed as you have an array mArrayList of cursor's data,

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
1

If it is short and static, you might consider putting it in your strings.xml as an array and accessing it that way rather than incurring somewhat greater overhead of reading from a DB and translating to an array.

data.xml

<resources>
    <string-array name="States">
        <item>AL</item>
        <item>AK</item>
        <item>AR</item>
    </string-array>
</resources>

Then to use it for your spinner:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
    getActivity(), R.array.States, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.listlayout_black);
final Spinner states = (Spinner) v.findViewById(R.id.mbr_state_spinner);
states.setAdapter(adapter);
Barak
  • 16,318
  • 9
  • 52
  • 84
0

Then you have to write a logic for iterating over cursor and fetch data. Create array with these data you got from cursor.

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

Here you go

ArrayList<String> list = new ArrayList<String>();
Cursor data = dataHandler.activeEmployeeList();

if (data.moveToFirst()) 
{
    do {
        list.add(data.getString(data.getColumnIndex(MyobiliseData.Columns_employees.NAME)));
       } while (data.moveToNext());
}
Vipul
  • 27,808
  • 7
  • 60
  • 75
  • I forgot to mention that in the cursor has an "id" field as well, which I need together with the name. Then when the user clicks on the name I can get the id. Is this possible? – WaterBoy Jun 19 '12 at 05:55