我想将3个微调器中的选定值保存到带有getter / setter的变量中。但是,当我试图显示价值时,它什么都没有回报。
这是设置方法
spinner_value(spinner, spinner1_selected);
setLang(getSpinner(spinner1_selected));
spinner_value(spinner2, spinner2_selected);
setCountry(getSpinner(spinner2_selected));
spinner_value(spinner3, spinner3_selected);
setCity(getSpinner(spinner3_selected));
尝试显示任何值
new AlertDialog.Builder(this)
.setTitle(getLang())
.setMessage(getCountry())
.show();
我的微调器到变量转换器
public void spinner_value(Spinner spinner, final String spinner_value)
{
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
setSpinner(item.toString(), spinner_value);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
我的getter \ setter
public void setSpinner(String input, String spinner) {
spinner = input;
}
public String getSpinner(String spinner) {
return spinner;
}
答案 0 :(得分:0)
通常你会得到这样的值(在你的情况下你想要一个字符串):
String text = spinner.getSelectedItem().toString();
但我真的不知道你想做什么。您的get / set方法只是返回或更改您提供的String值。 SetSpinner仅设置两个给定的值相等。 getSpinner只返回你给出的相同的字符串,这对我来说没有意义。
答案 1 :(得分:0)
试试这种方式,希望这可以帮助您解决问题。
<强> main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp">
<Spinner
android:id="@+id/spnLanguage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/language"
android:layout_marginTop="20dp"/>
<Spinner
android:id="@+id/spnCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/country"
android:layout_marginTop="20dp"/>
<Spinner
android:id="@+id/spnCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/city"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btnValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Value"/>
</LinearLayout>
<强> string.xml 强>
<string-array name="language">
<item>"Language 1"</item>
<item>"Language 2"</item>
<item>"Language 3"</item>
<item>"Language 4"</item>
<item>"Language 5"</item>
</string-array>
<string-array name="country">
<item>"Country 1"</item>
<item>"Country 2"</item>
<item>"Country 3"</item>
<item>"Country 4"</item>
<item>"Country 5"</item>
</string-array>
<string-array name="city">
<item>"City 1"</item>
<item>"City 2"</item>
<item>"City 3"</item>
<item>"City 4"</item>
<item>"City 5"</item>
</string-array>
<强> MyActivity.java 强>
public class MyActivity extends Activity {
private Spinner spnLanguage;
private Spinner spnCountry;
private Spinner spnCity;
private Button btnValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnValue = (Button) findViewById(R.id.btnValue);
spnLanguage = (Spinner) findViewById(R.id.spnLanguage);
spnCountry = (Spinner) findViewById(R.id.spnCountry);
spnCity = (Spinner) findViewById(R.id.spnCity);
btnValue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MyActivity.this)
.setTitle(spnLanguage.getSelectedItem().toString())
.setMessage(spnCountry.getSelectedItem().toString() +" "+spnCity.getSelectedItem().toString())
.show();
}
});
}
}