我正在研究android,我要做的就是这个:你有五个大陆和相对的国家/资本。创建一个带有两个 spinner 和 une button 的 Activity。当您从第一个微调器中选择大陆时,第二个微调器应仅显示相关大陆的状态。当您单击按钮时,应显示一个对话框: Continental: Selected Continental; 状态:选定状态;StateCapital:州首府。我不想使用任何数据库(我现在是个假人)你能帮我吗?我认为使用数组更简单。
我创建我的字符串 xml
<string name="Select_continent">Select Continent</string>
<string-array name="continent">
<item>Usa</item>
<item>Europe</item>
<item>Australia</item>
<item>Asia</item>
<item>Africa</item>
</string-array>
<string name="Select_state_usa">Select State</string>
<string-array name="Usa">
<item>Alabama</item>
<item>Ohio</item>
<item>Florida</item>
<item>...</item>
<string name="Select_state_eu">Select State</string>
<string-array name="Europe">
<item>Germany</item>
<item>Italy</item>
<item>England</item>
<item>...</item>
然后我创建我的 main.xml
<Spinner
android:id="@+id/spinner1"
android:layout_width="300dp"
android:layout_height="50dp"
android:entries="@array/continent"
android:prompt="@string/Select_continent"
android:gravity="center"
/>
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Select continent:"
android:textColor="#000" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="297dp"
android:layout_height="50dp"
/>
然后我创建 MyActivity.java
public class MyActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Myactivity);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
//add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("Austria");
list.add("Italy");
list.add("Germany");
list.add("France");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
this,android.R.layout.simple_spinner_item,list);dataAdapter.setDropDownViewResource
android.R.layout.simple_spinner_dropdown_item);spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {AlertDialog.Builder alertDialogBuilder3 = new AlertDialog.Builder
(context);
alertDialogBuilder3
.setTitle("Continent")
.setIcon(R.drawable.info)
.setMessage("Continent " + "\n " + String.valueOf (spinner1.getSelectedItem()) +
"\n " + String.valueOf(spinner2.getSelectedItem()) +
"\nCapital : " )
.setNeutralButton("Ok",null)
.create() // create one
.show();
}
});
}}
你能帮我将相对状态添加到大陆吗?