How to change spinner value with alert dialog? Android

1.1k Views Asked by At

Can someone tell me how to change the spinner value after the alert dialog is called ? I'm calling the alert dialog when "Other..." is clicked. I dont know how to replace the "Other..." value with the new one that I have written in the alert dialog.

    final Spinner spinnerLessonDuration = (Spinner) findViewById(R.id.spinnerLessonDuration);
    final ArrayAdapter<CharSequence> adapterLessonDuration = ArrayAdapter.createFromResource(this, R.array.lessonDuration, android.R.layout.simple_spinner_item);
    adapterLessonDuration.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerLessonDuration.setAdapter(
            new NothingSelectedSpinnerAdapter(
                    adapterLessonDuration,
                    R.layout.contact_spinner_row_nothing_selected_lesson_duration, this));

    spinnerLessonDuration.setOnItemSelectedListener(new setOnItemSelectedListener() {
           public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               System.out.println("2 " + spinnerLessonDuration.getSelectedItem());
               if (spinnerLessonDuration.getSelectedItem() != null && "Other...".equals(spinnerLessonDuration.getSelectedItem().toString()))
               {
                           View view1 = (LayoutInflater.from(MainActivity.this)).inflate(R.layout.user_input, null);

                           AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
                           alertBuilder.setView(view1);
                           final EditText userInput = (EditText) view1.findViewById(R.id.userinput);

                           alertBuilder.setCancelable(true)
                                   .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                                       @Override
                                       public void onClick(DialogInterface dialog, int which) {
                                           //EditText valueView = (EditText) view1.findViewById(R.id.license_value); //here
                                           if (userInput == null) Log.d("AA", "NULL");
                                           else {
                                               String value = userInput.getText().toString();
                                               Log.i(value, "1");


                                           }


                                       }
                                   });
                           Dialog dialog = alertBuilder.create();
                           dialog.show();

               }


           }

           }
     );
3

There are 3 best solutions below

0
On BEST ANSWER

FIXED IT!

Problem was that the arrayList from R.array was immutable :)

    ArrayList<CharSequence> testArrayList = new ArrayList<CharSequence>();
    CharSequence[] strings = getResources().getTextArray(R.array.lessonDuration);
    Collections.addAll(testArrayList, strings);

    final Spinner spinnerLessonDuration = (Spinner) findViewById(R.id.spinnerLessonDuration);
    final ArrayAdapter<CharSequence> adapterLessonDuration = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, testArrayList);
0
On

First you need to replace the item in your ListAdapter. You can remove and then insert a new value, like this:

adapterLessonDuration.setNotifyOnChange(false);
adapterLessonDuration.remove(adapterLessonDuration.getItem(position)); 
adapterLessonDuration.insert(userInput, position);
adapterLessonDuration.notifyDataSetChanged();

make position parameter final.

Note: after item value changed, your condition "Other...".equals(...) will not work anymore.

Another approaches - is to override ArrayAdapter to your own, or to put into it values of custom class, and to call notifyDataSetChanged() after item value changed.

0
On
.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testing.MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

.java file

public class MainActivity extends Activity {

    Spinner spinner;
    String[] name = { "Prakash", "Bhavin", "Chirag", "Kishan", "Sandy" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getApplicationContext(),
                android.R.layout.simple_dropdown_item_1line, name);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);


        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this);
                builder.setTitle("This is Selected Value " + name[position]);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(getApplicationContext(),
                                        "Yes Button clicked...",
                                        Toast.LENGTH_SHORT).show();

                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(getApplicationContext(),
                                        "No Button clicked...",
                                        Toast.LENGTH_SHORT).show();

                            }
                        });
                builder.setNeutralButton("Help",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(getApplicationContext(),
                                        "Help Button clicked...",
                                        Toast.LENGTH_SHORT).show();

                            }
                        });
                builder.show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

    }


}