我有3个下拉列表(微调),如何从第一个列表中选择项目时更新第二个列表?

时间:2014-03-06 20:23:43

标签: java android dropdownbox

你好,这是我的第一个问题,我是android的新手,我希望你能忍受我:)

我有3个下拉框列表。 第一个有3个项目,每个项目有3个不同的项目,这3个不同的项目有10个不同的项目。

那么,如果我从第一个框中选择项目将更新第二个列表,从第二个列表中选择将更新第三个下拉框,怎么可能?

同样在developer.android.com中,他们手动制作了数组。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
</string-array>
</resources>

有没有办法从数据库或类似的东西中获取列表内容?

这是我的strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AndroidSpinner</string>
    <string name="menu_settings">Settings</string>

    <string name="adatext">ADA NO</string>
    <string name="bloktext">BLOK NO</string>
    <string name="dairetext">DAİRE NO</string>

    <string name="select">Ada No</string>
    <string name="select2">Block No</string>
    <string name="select3">Daire No</string>
    <string name="button_label">Göster</string>

    <string-array name="ada_array">
        <item>ADA 290</item>
        <item>ADA 292</item>
        <item>ADA 295</item>

    </string-array>

    <string-array name="ada290_array">
        <item>DAİRE BLOK A</item>
        <item>DAİRE BLOK B</item>
        <item>DÜKKAN BLOK A</item>
        <item>DÜKKAN BLOK B</item>

    </string-array>

    <string-array name="ada290_daire_blok_a_array">
        <item>DAİRE NO 1</item>
        <item>DAİRE NO 2</item>
        <item>DAİRE NO 3</item>
        <item>DAİRE NO 4</item>
        <item>DAİRE NO 5</item>
        <item>DAİRE NO 6</item>
        <item>DAİRE NO 7</item>
        <item>DAİRE NO 8</item>
        <item>DAİRE NO 9</item>
        <item>DAİRE NO 10</item>
        <item>DAİRE NO 11</item>
        <item>DAİRE NO 12</item>
        <item>DAİRE NO 13</item>
        <item>DAİRE NO 14</item>
        <item>DAİRE NO 15</item>
        <item>DAİRE NO 16</item>
        <item>DAİRE NO 17</item>
        <item>DAİRE NO 18</item>
        <item>DAİRE NO 19</item>
        <item>DAİRE NO 20</item>
        <item>DAİRE NO 21</item>
        <item>DAİRE NO 22</item>
        <item>DAİRE NO 23</item>
        <item>DAİRE NO 24</item>
        <item>DAİRE NO 25</item>
        <item>DAİRE NO 26</item>
        <item>DAİRE NO 27</item>
        <item>DAİRE NO 28</item>
        <item>DAİRE NO 29</item>
        <item>DAİRE NO 30</item>
        <item>DAİRE NO 31</item>
        <item>DAİRE NO 32</item>
        <item>DAİRE NO 33</item>
        <item>DAİRE NO 34</item>
        <item>DAİRE NO 35</item>
        <item>DAİRE NO 36</item>


    </string-array>

</resources>

这是我从互联网上找到的示例代码MainActivity.java,来自添加到spinner2的代码manuel项目,但是如果从ada_array中选择ada290,我想将ada290_array添加到spinner2。我怎么能这样做?

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

  private Spinner spinner1, spinner2;
  private Button button;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    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("Item 1");
    list.add("Item 2");
    list.add("Item 3");
    list.add("Item 4");
    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);
    spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
  }

  public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        Toast.makeText(MainActivity.this,
        "Result : " + 
                "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) + 
                "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
            Toast.LENGTH_SHORT).show();
      }

    });
  }
} 

0 个答案:

没有答案