在我的活动中,我有两个微调器,取决于一个微调器,第二个微调器应该改变。就像我在第一个微调器中选择水果然后第二个微调器应该有下拉列表如苹果,芒果等。但是在两个微调器中选择项目后,我应该在布局的下方的编辑文本中获得输出。我已经完成了两者的编码,它正在解决的问题是,当我在第二个微调器中选择项目4时,第4个项目没有显示而是仅显示第一个选项。我在string.file中创建了数组。
package com.example.first;
import java.lang.reflect.Array;
import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner s1, s2;
// TextView t1;
EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner) findViewById(R.id.spinner1);
s2 = (Spinner) findViewById(R.id.spinner2);
ed=(EditText)findViewById(R.id.editText1);
s1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
long arg3) {
parent.getItemAtPosition(pos);
switch (pos) {
case 0:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Ismail,android.R.layout.simple_spinner_item);
// s2.setOnItemSelectedListener(this);
s2.setAdapter(adapter);
break;
case 1:
ArrayAdapter<CharSequence> adapter1= ArrayAdapter.createFromResource(this,
R.array.abdul,android.R.layout.simple_spinner_item);
s2.setAdapter(adapter1);
break;
case 2:
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.Mohammed,android.R.layout.simple_spinner_item);
s2.setAdapter(adapter2);
break;
}
Object result=s1.getSelectedItem();
Object result2=s2.getSelectedItem();
ed.setText(""+result +":"+result2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
答案 0 :(得分:0)
试试这个。
ArrayAdapter<String> ad = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Helper.country);
country.setAdapter(ad);
country.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Value_spinner1 = (String) parent.getItemAtPosition(position);
state.setEnabled(true);
Toast.makeText(getApplicationContext(),
"" + parent.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();
switch (Value_spinner1) {
case "INDIA":
india_state();
break;
case "pak":
pak_state();
break;
case "Sri Lanka":
sri_lanka_state();
break;
default:
// state.setVisibility(View.GONE);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
state.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Value_spinner2 = (String) parent.getItemAtPosition(position);
ed.setText(Value_spinner1 + Value_spinner2);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void sri_lanka_state() {
// TODO Auto-generated method stub
ArrayAdapter<String> state_spinner = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Helper.Sri_Lanka_State);
state.setAdapter(state_spinner);
}
public void pak_state() {
// TODO Auto-generated method stub
ArrayAdapter<String> state_spinner = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Helper.pak_state);
state.setAdapter(state_spinner);
}
public void india_state() {
// TODO Auto-generated method stub
ArrayAdapter<String> state_spinner = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Helper.INDIA_STATE);
state.setAdapter(state_spinner);
}
答案 1 :(得分:0)
这是解决方案
// first create string variables to store selected spinner item values and Id values
// First list model has two things, 1.Name and 2.FirstItemId
// Second list model had three things, 1.Name 2.Id and 3.FirstItemId
String st_firstSpinnerSelectedItem, st_secondSpinnerSelectedItem,st_firstSpinnerSelectedItemID,st_secondSpinnerSelectedItemID;
ArrayList<FirstListModel> firstList=new ArrayList<>();
ArrayList<SecondListMode> secondList = new ArrayList<>();
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()){
case R.id.spnFirst:
st_firstSpinnerSelectedItem = firstList.get(position).getName();
st_firstSpinnerSelectedItemID = firstList.get(position).getFirstItemId();
setSecondSpinner(st_firstSpinnerSelectedItemID);
break;
case R.id.spnSecond:
st_secondSpinnerSelectedItem = secondList.get(position).getName();
st_secondSpinnerSelectedItemID = secondList.get(position).getId();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
// write below code out side of the onCreate()
private void setSecondSpinner(String st_firstSpinnerSelectedItemID) {
ArrayList<SecondListModel> selectedSecondListItems = new ArrayList<SecondListModel>();
for (int i=0; i<secondList.size();i++){
String secondSpinnerItemId = secondList.get(i).getFirstItemId();
if (secondSpinnerItemId .equals(st_firstSpinnerSelectedItemID)){
selectedSecondListItems .add(secondList.get(i));
}
}
SecondSpinAdapter secondAd = new SecondSpinAdapter(context, selectedSecondListItems );
spnSecond.setAdapter(seconddAd );
}