将微调器的内容动态更改为另一个微调器项

时间:2016-03-21 13:14:07

标签: android spinner android-spinner

我有旋转器,旋转器和spinner2,第一个有标准菜单,第二个必须相对于微调器中的项目进行更改。

我试图获取在微调器中选择的项目的位置,然后将其作为参数放入我的方法item2sp中以更改微调器的2个项目。

但是我没有改变,我一直只在spinner2中获得第一个菜单菜单,我该如何修复它可以帮助我。

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

Spinner spinner2;

List<String> categories;
List<String> ndp;
List<String> ps;
ArrayAdapter<String> dataAdapter;
ArrayAdapter<String> ndpAdapter;
ArrayAdapter<String> psAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);

// Spinner click listener
spinner.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);

// Spinner Drop down elements
categories = new ArrayList<String>();
categories.add("NDP");
categories.add("PS");
categories.add("PA");
categories.add("PC");
categories.add("PH");

ps = new ArrayList<String>();
ps.add("AA");
ps.add("BB");
ps.add("CC");

ndp = new ArrayList<String>();
ndp.add("RP");
ndp.add("AFF");
ndp.add("RPA");




// Creating adapter for spinner
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
psAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ps);
ndpAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ndp);

// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
psAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ndpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);

}

public void itemsp2 (int position){
switch (position){
case 0:
spinner2.setAdapter(ndpAdapter);
Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();

break;
case 1:
spinner2.setAdapter(psAdapter);
Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
break;
}


}
// int position = spinner.getSelectedItemPosition();

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
itemsp2(position);

// Showing selected spinner item
// Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}


public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

1 个答案:

答案 0 :(得分:0)

iller是我的第一个列表,我将此列表设置为我的第一个微调器,然后我为此微调器实现onItemSelected方法。在此方法中,您可以使用不同的List添加第二个微调器。

 Spinner spinner = (Spinner) findViewById(R.id.spinner);
            spinner.setOnItemSelectedListener(this);
            List<String> iller = new ArrayList<String>();
            iller.add("İl Seçiniz..");
            iller.add("Antalya");
            iller.add("Burdur");
            iller.add("Bursa");
            iller.add("Balıkesir");
            iller.add("Giresun");
            iller.add("İstanbul");
            ArrayAdapter<String> illerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, iller);
            illerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(illerAdapter);

            spinner2 = (Spinner) findViewById(R.id.spinner2);
            spinner2.setVisibility(View.INVISIBLE);

            editText = (EditText) findViewById(R.id.editText);
            editText.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            int itemId = (int) id;
            if (itemId == 1) {
                iller.remove("İl Seçiniz..");
                antalyaİlceler = new ArrayList<String>();
                antalyaİlceler.add("İlçe Seçiniz..");
                antalyaİlceler.add("Konyaaltı");
                antalyaİlceler.add("Muratpaşa");
                antalyaİlceler.add("Alanya");
                antalyaİlceler.add("Manavgat");

                ArrayAdapter<String> antalyaİlcelerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, antalyaİlceler);
                antalyaİlcelerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner2.setAdapter(antalyaİlcelerAdapter);
                spinner2.setVisibility(View.VISIBLE);
                editText.setVisibility(View.VISIBLE);
            } else if (itemId == 2) {
                burdurİlceler = new ArrayList<String>();
                burdurİlceler.add("İlçe Seçiniz..");
                burdurİlceler.add("Bucak");
                burdurİlceler.add("Tefenni");
                burdurİlceler.add("Gölhisar");
                burdurİlceler.add("Ağlasun");
                ArrayAdapter<String> burdurİlcelerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, burdurİlceler);
                burdurİlcelerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner2.setAdapter(burdurİlcelerAdapter);
                spinner2.setVisibility(View.VISIBLE);
                editText.setVisibility(View.VISIBLE);
            }