How to update data in Spinner Dynamically in Android? [duplicate]
Asked Answered
D

3

5

i have added my data in array.xml file and link to

 <string-array name="state">
        <item>ANDAMAN NICOBAR ISLANDS</item>
        <item>ANDHRA PRADESH</item>
        <item>ARUNACHAL PRADESH</item>

  <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/drpstate"
        android:entries="@array/state"/>

i want to change this entries dynamically

thanks in advance ..

Derouen answered 3/8, 2016 at 8:35 Comment(5)
then use an adapter that accepts dynamic dataDiscretion
@Discretion Please Privide Some Example.Derouen
ArrayAdapter for exampleDiscretion
Possible duplicate of How to update a spinner dynamically in Android? #9443870Answerable
@NatheemSafin please don't add "thank you" or other signatures in your posts. It adds noise. Also avoid using irrelevant tags, i.e tags which do not depict the actual problems you're facing. In your case, Android Studio is irrelevant.Superannuate
A
12

Use adapter.add() to add data and then call adapter.notifyDataSetChanged() to apply changes.

    Spinner spinner = (Spinner)findViewById(R.id.mySpinner);
    List<String> list = Arrays.asList(getResources().getStringArray(R.array.state));

    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerAdapter);
    spinnerAdapter.add("DELHI");
    spinnerAdapter.notifyDataSetChanged();
Audacity answered 3/8, 2016 at 8:48 Comment(0)
D
4

You can achieve this in Java code as follows:

  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);

  spinner.setAdapter(dataAdapter);

Source: http://www.tutorialspoint.com/android/android_spinner_control.htm

Dasha answered 3/8, 2016 at 8:38 Comment(3)
thank you man.. without adding in java... use this xml File Dynamically its possible ??Derouen
No, it's not. However, if you want to read new list from XML string-array, you can do it as follows: String[] testArray = getResources().getStringArray(R.array.testArray);Dasha
hey thank you man its worksDerouen
B
1

You do this using a any subclass of the BaseAdapter. Assuming you want to load data from the database you use the SimpleCursorAdapter.

    String[] from = new String[]{"nameOfColumn"}; //nameOfColumn is the name of the column in cursor to display in the spinner. 
    int[] to = new int[]{android.R.id.text1};

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter (this, android.R.layout.simple_spinner_item, null, from, to, 0);
    cursorAdapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
    binding.paymentChannelSpinner.setAdapter (cursorAdapter);
    binding.paymentChannelSpinner.setOnItemSelectedListener (new AdapterView.OnItemSelectedListener () {
        @Override
        public void onItemSelected (AdapterView<?> adapterView, View view, int i, long l) {
            if (i != -1) {
                Cursor c = (Cursor) adapterView.getItemAtPosition (i);

            }
        }

        @Override
        public void onNothingSelected (AdapterView<?> adapterView) {

        }
    });

after getting your data as a cursor from the database you will set the cursor to the adapter like this:

   cursorAdapter.changeCursor(cursorFromDB); 

However, if the data is in a java array you can user an Arrayadapter

Buddhology answered 3/8, 2016 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.