[英]How to save the display information between spinners android
我有一个显示三个微调器的android活动。 相同的三个具有相同的信息可供选择,因为您应该在五个中选择三个。 我要做的是根据当前选择的内容显示信息。 例如,如果我在第一个微调器中选择了“硬币”,我不想在剩下的两个微调器中选择硬币,如果在第二个微调器中选择了“资本”,则我不想在其中显示大写和硬币最后一个。
这是我的活动的xml:
<?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 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();
}
这就是我的范围
<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>
我一直在谷歌和stackoverflow上搜索,但我找不到我想要的东西,或者只是我不知道如何正确询问
非常感谢!
使用onitemselected方法将Adapter动态设置为微调框
我发现的正是我所寻找的。 问题是我有一个ArrayAdapter,该ArrayAdapter在由数组初始化时将其转换为不能修改的AbstractList(List)。
解决方案在初始化ArrayAdapter时,请使用ArrayList而不是使用数组。
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
}
});
我现在想知道要删除的内容以将其添加到下一个微调器中时遇到的问题,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.