---
# System prepended metadata

title: homework drop list
tags: [android]

---

# homework drop list

###### tags: `android`




## String Resource File.

```
<resources>  
 <string name="app_name">7-21下拉選單</string>  
 <string-array name="dessert">  
 <item>蘋果派</item>  
 <item>可麗餅</item>  
 <item>奶酪</item>  
 <item>現烤鬆餅</item>  
 <item>冰淇淋</item>  
 </string-array></resources>
```


## Main program

```java=
package com.example.a7_21;  
  
import androidx.appcompat.app.AppCompatActivity;  
  
import android.os.Bundle;  
import android.view.View;  
import android.widget.ArrayAdapter;  
import android.widget.Spinner;  
import android.widget.TextView;  
  
 public class MainActivity extends AppCompatActivity {  
  
    private Spinner sp1,sp2;  
 private String\[\] desserts;  
  
   @Override  
  
   protected void onCreate(Bundle savedInstanceState) {  
  
        String\[\] courses={"漢堡","牛排","牛肉飯","義大利麵"};  
  
 super.onCreate(savedInstanceState);  
   setContentView(R.layout.activity_main);  
  
   //Primary components  
  
   sp1=findViewById(R.id.spinner);  
  
   //Create an Array adapter object  
   ArrayAdapter<String> a1 =new ArrayAdapter<>(this, android.R.layout.simple\_spinner\_dropdown_item,courses);  
   sp1.setAdapter(a1);  
  
   //specify the adapter object  
 //components of the dessert   desserts = getResources().getStringArray(R.array.dessert);  
   sp2 = findViewById(R.id.spinner2);  
   ArrayAdapter<String>a2 = new ArrayAdapter<>(this, android.R.layout.simple\_spinner\_dropdown_item,desserts);  
   sp2.setAdapter(a2);  
  
   }  
    public void button_Click(View view){  
        //get options which was selected by user  
  
   String main = sp1.getSelectedItem().toString();  
   String dessert = sp2.getSelectedItem().toString();  
   TextView output = findViewById(R.id.lblOutput);  
   output.setText("primary food"+main+"\\n dessert:"+dessert);  
   }  
}
```

---


Result 
![](https://i.imgur.com/7Jy7na5.png)
