我有以下疑问我必须在我的项目中使用一些数组列表...
列表餐厅菜肴餐厅1菜肴餐厅2价格餐厅1价格餐厅2
在我的活动中,我放了两个Spinners,一个将获得Array List Restaurants,另一个我希望它在我选择餐厅时显示Array列表的数据,最后我会选择和setaria作为文本查看价格。
只是为了了解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>
答案 0 :(得分:0)
尝试下面的代码,我已经解释并写了..
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) {
}
});