2

I have a dropdown which takes its values from a database, I then want to add a default value to the existing drop down values. In this case the default value would be the selected value.

 StringBuilder strbuilder = new StringBuilder();
    ArrayList<String> arr_list = new ArrayList<String>();
    List<String> m = new ArrayList<String>();

    //This get the data values from database
    m = db.getData();

    int ms = m.size();
    String str = strbuilder.toString();

    String[] str1 = str.split(",");


    try {
        if (ms > 1) {

            //This is my default value
             m.add("Default Value");

            for (int i = 0; i < ms; i++) {

                m.get(0);
            }
        } else {
            m.get(0);

        }
    } catch (Exception e) {
        e.printStackTrace();

    }


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, m);
    dropdown.setAdapter(adapter);

The codes above is able to add the values from the database to the dropdown and the Default Value is added but it doesn't appear to be the selected value.

For instance if the values from the database are "Apple, Mango,Pineapple" and the default value is "ALL FRUITS".

EXPECTED DROPDOWN

ALL FRUITS Apple Mango Pineapple

Please how can I do make the default value the selected value? Thanks in advance.

George
  • 1,086
  • 14
  • 48
  • Can you be a bit more clear what you want to achieve?? I cant understand your question. Do you want a extra data to be added to spinner after getting all data from database and keep that one selected??? – Zahan Safallwa Feb 13 '16 at 13:21

2 Answers2

3

I think you need below code.

    List<String> categories = new ArrayList<String>();
    categories = db.getData();
    categories.add(0,"your_default_value");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
    spinner.setAdapter(dataAdapter);
    spinner.setSelection(0);

Remember when you want to add any value to the very first of Arraylist you just do

 arraylist.add(0,"value");

This will add the value to the very first index of arraylist and push all other element down.

In case you need any other element to be selected not first one

 Spinner spinner = (Spinner) findViewById(R.id.spinner);
    List<String> categories = new ArrayList<String>();
    categories.add("Automobile");
    categories.add("Business Services");
    categories.add("Computers");
    categories.add("Education");
    categories.add("Personal");
    categories.add("Travel");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);
    int i=categories.indexOf("Education");
    spinner.setSelection(i);
Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32
  • Excellent answer, I knew you could do it and even better, Thanks so well really appreciate – George Feb 13 '16 at 14:10
  • Please I'm a bit stucked on a problem, please if you are less busy kindly try your hands on it for me. http://stackoverflow.com/q/35526327/2595059 Thank You – George Feb 20 '16 at 17:53
0

If the default value is always going to be at the 0 index, just get a reference to your dropdown/spinner, then set:

spinner.setSelection(0);

And here is a previously answered question detailing how to set it by value, if you'd rather do that. https://stackoverflow.com/a/4228121/3299157

Community
  • 1
  • 1
Ageonix
  • 1,748
  • 2
  • 19
  • 32