简体   繁体   中英

How to save the display information between spinners android

I have an android activity that shows three spinners. The same three have equal information to choose, because you are supposed to choose three things among five. what I want to do is display the information based on what is selected now. For example if in the first spinner I selected "Coins" I do not want to be able to select coins in the two spinners left, if in the second spinner you selected "Capital" I don't want to show either capital and coins in the last one.

this is my xml for the activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#449def"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/typeDrinks"
            style="@style/textStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select three question for ask"
            android:textSize="22sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/LinearLayout" >

        <Spinner
            android:id="@+id/spinner_type1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8sp"
            android:gravity="center" />
    </LinearLayout>

    <TextView
        android:id="@+id/textRangeOfPrices"
        style="@style/textStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#449def"
        android:gravity="center"
        android:text="Select another one"
        android:textSize="22sp" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textRangeOfPrices"
        android:orientation="horizontal" >

        <Spinner
            android:id="@+id/spinner_type2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8sp"
            android:gravity="center" />
    </LinearLayout>

    <TextView
        android:id="@+id/textViewDistance"
        style="@style/textStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#449def"
        android:gravity="center"
        android:text="The last one"
        android:textSize="22sp" />

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textViewDistance"
        android:orientation="horizontal" >

        <Spinner
            android:id="@+id/spinner_type3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8sp"
            android:gravity="center" />
    </LinearLayout>

    <Button
        android:id="@+id/buttonGenerateRestaurant"
        style="@style/ButtonText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@layout/blue_button"
        android:onClick="generateQuery"
        android:text="Generate" />

</RelativeLayout>

this is my code so far. With this code the same information appears on the three spinners, and you can choose three times the same thing

/*
         * this part of the code is for displaying the spinner with the selected
         * choice
         */
        spinnerType1 = (Spinner) findViewById(R.id.spinner_type1);
        spinnerType2 = (Spinner) findViewById(R.id.spinner_type2);
        spinnerType3 = (Spinner) findViewById(R.id.spinner_type3);

        ArrayAdapter<CharSequence> adapterType1 = ArrayAdapter
                .createFromResource(this, R.array.range_types,
                        android.R.layout.simple_spinner_item);

        ArrayAdapter<CharSequence> adapterType2 = ArrayAdapter
                .createFromResource(this, R.array.range_types2,
                        android.R.layout.simple_spinner_item);

        ArrayAdapter<CharSequence> adapterType3 = ArrayAdapter
                .createFromResource(this, R.array.range_type3,
                        android.R.layout.simple_spinner_item);

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

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

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

        spinnerType1.setAdapter(adapterType1);
        spinnerType2.setAdapter(adapterType2);
        spinnerType3.setAdapter(adapterType3);

        spinnerType1.setOnItemSelectedListener(this);
        spinnerType2.setOnItemSelectedListener(this);
        spinnerType3.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
        mType1 = (String) spinnerType1.getSelectedItem().toString();
        mType2 = (String) spinnerType2.getSelectedItem().toString();
        mType3 = (String) spinnerType3.getSelectedItem().toString();

    }

This is what I have on ranges

<string-array name="range_types">
    <item>Complete Name</item>
    <item>Coin</item>
    <item>Capital</item>
    <item>Area(Km)</item>
    <item>Political system</item>
</string-array>
<string-array name="range_types2">
    <item>Complete Name</item>
    <item>Coin</item>
    <item>Capital</item>
    <item>Area(Km)</item>
    <item>Political system</item>
</string-array>
<string-array name="range_type3">
    <item>Complete Name</item>
    <item>Coin</item>
    <item>Capital</item>
    <item>Area(Km)</item>
    <item>Political system</item>
</string-array>

I've been searching on google and stackoverflow but I don't find exactly what I am looking for, or just I don't know how to ask it correctly

Thank you so much!

使用onitemselected方法将Adapter动态设置为微调框

I found it exactly what I was looking. The problem was that I had an ArrayAdapter, The ArrayAdapter, on being initialized by an array, converts the array into a AbstractList (List) which cannot be modified.

Solution Use an ArrayList instead using an array while initializing the ArrayAdapter.

    String[] arrayShow = getResources().getStringArray(R.array.range_types);

            ArrayList<String> lst = new ArrayList<String>();
            lst.addAll(Arrays.asList(arrayShow));


            final ArrayAdapter<String> adapterType1 = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, lst);

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


        spinnerType1.setAdapter(adapterType1);

spinnerType1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                adapterType2.remove(adapterType1.getItem(arg2));
                adapterType2.notifyDataSetChanged();

            }

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


            }
        });

The problem that I have now if that I want to keep track of what I am deleting for adding it to the next spinner and so on..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM