0

SOLVED HERE IS THE SOLUTION ANSWER http://www.congdegnu.es/2011/06/02/spinners-en-android-tres-formas-de-poblarlos/ I'm populating a spinner from my sqlite database like this:

Cursor CS = newDB.rawQuery("Select ID AS _id, Name from Schools",null);
    CS.moveToFirst();
    do{     
        Schools.add(CS.getString(CS.getColumnIndex("_id")));   
    } while(CS.moveToNext());

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

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner1.setAdapter(dataAdapter);

My problem is that I only add an id but how can I add a value to that id so when the value get select I get the id ?

user2033349
  • 175
  • 1
  • 2
  • 15

4 Answers4

0

Take a look at using a SimpleCursorAdapter for your spinner instead. Although this has been deprecated since API-11, so you may also want to think about using a LoaderManager with a CursorLoader.

An explanation on how to transition to a LoaderManager and CursorLoader can be found here: How to transition from managedQuery to LoaderManager/CursorLoader?

I would recommend you start by using the SimpleCursorAdapter, then if confortable enough, move on to the other method.

Community
  • 1
  • 1
jimmithy
  • 6,360
  • 2
  • 31
  • 29
  • I try this SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,CS,new String[] {"Name"},new int[] {android.R.id.text1}); and is not showing data – user2033349 Mar 05 '13 at 16:55
  • I found my answer here ANSWER http://www.congdegnu.es/2011/06/02/spinners-en-android-tres-formas-de-poblarlos/ – user2033349 Mar 05 '13 at 19:54
0

I think that "schools" is an array containing you own class "school".
You could add a propertie which contains the information.
Or you could use a 2 dimensional array.
Than you have to set up your arrayadapter to get the information (getter Method).
Hope it helps

j0chn
  • 1,103
  • 1
  • 7
  • 13
0

Try Cursor Adapter

or

Take a global variable

 Hashmap schoolmap=new Hashmap(); 

While inserting data to the list also add to Hashmap like this

CS.moveToFirst();
    do{     
        Schools.add(CS.getString(CS.getColumnIndex("_id")));   
        schoolmap.put(CS.getString(CS.getColumnIndex("_id")),CS.getColumnIndex("_id"))
    } while(CS.moveToNext());

onSpinner item click get the value like this

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

         public void onItemSelected(AdapterView<?> parent, View view, 
                    int pos, long id) {
              String value=((TextView)view).getText();
              int id=Integer.parseInt(schoolmap.get(value));
            }

            public void onNothingSelected(AdapterView<?> parent) {

            }

    }); 
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • i don't understand so i have to add the hash map to my arrayadapter? – user2033349 Mar 05 '13 at 16:56
  • @user2033349 no not like that.....map the values you are showing in the spinner as keys and the respective IDs as values in Hashmap, And from the spinner selection you'll get the value, pass that value as a key to hashmap, so it will give you the respective Id – Pragnani Mar 05 '13 at 17:27
0

1) Create an ArrayList<School>

2) Add all your Schools to the Arraylist

3) Create a custom adapter like this:

public class SchoolAdapter extends ArrayAdapter<School>{

    public SchoolAdapter(Context ctx, List<School> schools){
        super(ctx, 0, schools);
    }


    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {

        TextView tv;
        if (convertView == null){
            tv = new TextView(getContext());
        } else {
            tv = (TextView) convertView;
        }
        tv.setTextSize(16);
        final School school = getItem(position);
        tv.setText(school.toString());
        return tv;
    }

}

4) Use this in your activity:

final SchoolAdapter adapter = new SchoolAdapter(this, schools);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

     public void onItemSelected(AdapterView<?> parent, View view, 
                int pos, long id) {
         School school = adapter.getItem(pos);
         // Do whatever you want with your school            

     }

     public void onNothingSelected(AdapterView<?> parent) {

     }

}); 
Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90