Ajay Kulkarni
Ajay Kulkarni

Reputation: 3039

change values in 2nd dropdown based on another dropdown android

I've 2 drop downs in Android. I want to change contents of 2nd drop down based on values selected in 1st drop down. Here is the code.

<string-array name="categoriesSpinner">
        <item>ACCESS</item>
        <item>AVAILABILITY - PERFORMANCE</item>
        <item>FUNCTIONALITY</item>
        <item>INQUIRY</item>
        <item>DATA ERROR</item>
        <item>ERROR MESSAGE</item>
    </string-array>  

UI

    <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/categorySpinner"
            android:layout_width="match_parent"
            android:layout_height="45sp"
            android:hint="Category"
            android:layout_marginTop="@dimen/margin_small"
            android:background="@drawable/bg_drawable" />
<android.support.v7.widget.AppCompatSpinner
            android:id="@+id/subcategorySpinner"
            android:layout_width="match_parent"
            android:layout_height="45sp"
            android:hint="Sub-category"
            android:layout_marginTop="@dimen/margin_small"
            android:background="@drawable/bg_drawable" />  

Java:

public AppCompatSpinner mTextView, getmTextView;  
//AppCompatSpinner
        mTextView = findViewById(R.id.categorySpinner);
        String[] categories = getResources().getStringArray(R.array.categoriesSpinner);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, categories);
        arrayAdapter.notifyDataSetChanged();
        mTextView.setAdapter(arrayAdapter);
        String option = String.valueOf(mTextView.getSelectedItem());

        getmTextView = findViewById(R.id.subcategorySpinner);
        if (option.contentEquals("ACCESS")) {
            List<String> list = new ArrayList<>();
            list.add("ACCOUNT LOCKED");
            list.add("RESET PASSWORD");
            ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
            arrayAdapter1.notifyDataSetChanged();
            getmTextView.setAdapter(arrayAdapter1);
        }
        if (option.contentEquals("AVAILABILITY - PERFORMANCE")) {
            List<String> list = new ArrayList<>();
            list.add("LIMITED - DEGRADED");
            list.add("UNAVILABLE - DOWN");
            ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
            stringArrayAdapter.notifyDataSetChanged();
            getmTextView.setAdapter(stringArrayAdapter);
        }  

When I run the code in my android device, value of 2nd drop down doesn't change when clicked on 2nd value in first drop down. How can I fix it?

Upvotes: 1

Views: 1433

Answers (1)

sanjeev
sanjeev

Reputation: 1675

Move this code to onItemSelected of spinner.

mTextView.setOnItemSelectedListener(//the remaining code

String option = String.valueOf(mTextView.getSelectedItem());  //Don't forget to move this here otherwise it won't be updated.
    if (option.contentEquals("ACCESS")) {
                List<String> list = new ArrayList<>();
                list.add("ACCOUNT LOCKED");
                list.add("RESET PASSWORD");
                ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
                arrayAdapter1.notifyDataSetChanged();
                getmTextView.setAdapter(arrayAdapter1);
            }
            if (option.contentEquals("AVAILABILITY - PERFORMANCE")) {
                List<String> list = new ArrayList<>();
                list.add("LIMITED - DEGRADED");
                list.add("UNAVILABLE - DOWN");
                ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
                stringArrayAdapter.notifyDataSetChanged();
                getmTextView.setAdapter(stringArrayAdapter);
);

Upvotes: 2

Related Questions