简体   繁体   English

从另一个微调器填充微调器

[英]Populating Spinner from another Spinner

I have the following doubt I have to use some Arrays List in my project they are ... 我有以下疑问,我必须在我的项目中使用一些数组列表,它们是...

List Restaurant Dishes Restaurant 1 Dishes Restaurant 2 Price Restaurant 1 Price Restaurant 2 清单餐厅菜餐厅1菜餐厅2价格餐厅1价格餐厅2

Within my Activity I put two Spinners one that will get the Array List Restaurants and the other the I want it to appear the data of the Array list when I select the restaurant and in the end I would pick and setaria as a Text View of the price. 在我的“活动”中,我放置了两个微调框,一个将获得数组列表餐厅,另一个希望在选择餐厅时显示数组列表的数据,最后我将选择setaria作为文本视图。价钱。

在此处输入图片说明

Just to get an idea of ​​what's being populated in Spinner 只是为了了解Spinner中正在填充什么

<string-array name="restaurant">
    <item>McDonalds</item>
    <item>KFC</item>
</string-array>

<string-array name="restaurant_McDonalds_Plate">
    <item>Combo Big Mac</item>
    <item>Combo Deluxe Bacon</item>
    <item>Combo Club House</item>
</string-array>

<string-array name="Restaurant_McDonalds_Plate_Price">
    <item>R$ 31,00</item>
    <item>R$ 29,00</item>
    <item>R$ 35,00</item>
</string-array>

<string-array name="Restaurant_KFC_Plate">
    <item>Combo Balde de 6 peças</item>
    <item>Combo Balde de 9 peças</item>
    <item>Combo Balde de 12 peças</item>
</string-array>

<string-array name="Restaurant_KFC_Plate_Price">
    <item>R$ 25,00</item>
    <item>R$ 35,00</item>
    <item>R$ 45,00</item>
</string-array>

try below code , i have explained and wrote it .. 尝试下面的代码,我已经解释并写了..

    final Spinner retaurant;
    final Spinner plate;

    final TextView txt_retaurant;
    final TextView price;

    retaurant.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String restaurantname = retaurant.getSelectedItem().toString();

            txt_retaurant.setText(restaurantname);

            //if McDonalds, populates plate spinner with respective
            if(position == 0){
                ArrayAdapter<String> adapter= new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.restaurant_McDonalds_Plate));
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                plate.setAdapter(adapter);
            }
            //if kfc, populates plate spinner  with respective
            if(position == 1){
                ArrayAdapter<String> adapter= new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Restaurant_KFC_Plate));
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                plate.setAdapter(adapter);
            }

        }

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

        }
    });

    plate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            // if McDonalds selected , it gets respective price list of plate and sets the price list to textview according to selected value in plate spinner
            if(retaurant.getSelectedItemPosition() == 0){
                List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.Restaurant_McDonalds_Plate_Price));
                price.setText(myArrayList.get(position));
            }
            // if kfc selected , it gets respective price list of plate and sets the price list to textview according to selected value in plate spinner
            if(retaurant.getSelectedItemPosition() == 1){
                List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.Restaurant_KFC_Plate_Price));
                price.setText(myArrayList.get(position));
            }
        }

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

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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