两个微调器和阵列

时间:2013-07-30 13:15:18

标签: arrays spinner

我正在研究android和我要做的练习就是这个: 你拥有相对州/资本的五大陆军。 使用两个微调器和非按钮创建一个Activity。 从第一个微调器中选择“大陆”时,第二个微调器应仅显示相对大陆的状态。当您单击按钮时,应显示一个对话框: 美国大陆航空:选定的Contiental; 国家:选定国家; 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();
}
});

}}

你能帮我把相对状态添加到非洲大陆吗?

1 个答案:

答案 0 :(得分:0)

初始化两个微调器,填充大陆微调器并在所选项目上设置OnItemSelected()侦听器,switch-case,根据情况填充第二个微调器。

<强>编辑:

当然!请注意,但是我目前在平板电脑上,所以在将代码发送给您之前,我将无法测试代码。

public class MyClass extends Activity 
{

    ArrayList<String> continents; 
    ArrayList<String> statesOfContA; 
    ArrayList<String> statesOfContB;

    Spinner conts;
    Spinner states;

    Button btnShowToast;

    String continentValue;
    String stateValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        btnShowToast = (Button)findViewById(R.id.btnShow);

        continents = new ArrayList<String>();
        statesOfContA = new ArrayList<String>();
        statesOfContB = new ArrayList<String>();

        continents.add("Continent A");
        continents.add("Continent B");

        statesOfContA.add("State A");
        statesOfContA.add("State B");

        statesOfContB.add("State A");
        statesOfContB.add("State B");

        conts = (Spinner)findViewById(R.id.spConts);
        states = (Spinner)findViewById(R.id.spStates);

        ArrayAdapter<String> contAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, continents);
        conts.setAdapter(contAdapter);

        conts.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int pos,
                long arg3)
            {
                ArrayAdapter<String> stateAdapter;
                String choice = parent.getItemAtPosition(pos).toString();
                continentValue = continents.getSelectedItem().toString();

                switch(choice)
                {
                    case "Continent A":
                        stateAdapter = new ArrayAdapter<String>(MyClass.this, android.R.layout.simple_spinner_item, statesOfContA);
                        states.setAdapter(stateAdapter);
                        break;
                    case "Continent B":
                        stateAdapter = new ArrayAdapter<String>(MyClass.this, android.R.layout.simple_spinner_item, statesOfContB);
                        states.setAdapter(stateAdapter);
                        break;
               }
               Toast t = Toast.makeText(getApplicationContext(), "Continent: " + continentValue + " State: " + stateValue, Toast.LENGTH_LONG;
               t.show();
           }

        });

    btnShowToast.setOnClickListener(new View.setOnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Toast t = Toast.makeText(getApplicationContext(), "Continent: " continentValue + " State: " + stateValue, Toast.LENGTH_LONG;
            t.show();
        }
    }
}
}

注意任何拼写错误!如果这不起作用,请告诉我,以便我们找到另一种解决方案:)

祝你好运!