0

我正在研究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();
}
});

}}

你能帮我将相对状态添加到大陆吗?

4

1 回答 1

0

初始化两个微调器,填充大陆微调器并设置OnItemSelected()侦听器,在所选项目上切换大小写,根据案例填充第二个微调器。

编辑:

当然!但是请注意,我目前正在使用平板电脑,因此在将代码分发给您之前,我将无法对其进行测试。

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();
        }
    }
}
}

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

祝你好运!

于 2013-07-30T13:19:12.280 回答