我正在使用两个微调器,其中第二个微调器值基于第一个微调器值。
例如,如果用户在“第一个微调器”中选择选项A,则“第二个微调器”下拉列表必须包含值(牛,牛,山羊),如果用户选择选项B,则第二个微调器值必须包含(狮子,老虎和奇特斯)。
但是我无法这样做。在第一个微调器值中选择任何值时,第二个微调器始终加载相同的值。下面是屏幕截图
如您所见,在选择第一个微调器母版或文凭中的任何选项时...在第二个微调器中,值相同。它不会更改。
下面是我的活动代码
public class UploadBook extends AppCompatActivity
Spinner spinnerCountry,spinnerDivision,spinner_condition;
ArrayAdapter<String> countryArray,divisionArray;
String item = "start";
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_book);
getSupportActionBar().setTitle("Upload book");
spinnerCountry = (Spinner) findViewById(R.id.spinnerCountry);
//implementing OnItemSelectedListener (need to override the method)
spinnerCountry.setOnItemSelectedListener(this);
countryArray = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
countryArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCountry.setAdapter(countryArray);
countryArray.add("Master");
countryArray.add("Diploma");
countryArray.setNotifyOnChange(true);
spinnerCountry.setSelection(0);
spinnerDivision = (Spinner) findViewById(R.id.spinnerDivision);
//implementing OnItemSelectedListener (need to override the method)
spinnerDivision.setOnItemSelectedListener(this);
divisionArray = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
divisionArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDivision.setAdapter(divisionArray);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
item = parent.getItemAtPosition(position).toString();
count = position;
//first spinner item position
int countrySpinnerPosition = spinnerCountry.getSelectedItemPosition();
switch (countrySpinnerPosition){
case 1:
//fill data for second spinner
fillMasterDivision();
break;
case 2:
//fill data for second spinner
fillDiplomaDivision();
break;
}
}
private void fillMasterDivision() {
divisionArray.clear();
divisionArray.add("Select Subject:-");
divisionArray.add("MSC Physics");
divisionArray.add("MSC Chemistry");
divisionArray.notifyDataSetChanged();
}
private void fillDiplomaDivision() {
divisionArray.add("Select Subject:-");
divisionArray.add("Science");
divisionArray.add("CTEVT");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
请帮助。
答案 0 :(得分:2)
您的代码中有两个错误。
对于您的案例,CountrySpinnerPosition将为0和1,因此案例应为“案例0”和“案例1”。
在fillDiplomaDivision方法中,您必须首先清除列表,然后将其添加到列表中,然后调用notifyDataSetChanged()。
答案 1 :(得分:1)
尝试清除数组列表并在第二种方法中更改notifydataset
private void fillDiplomaDivision() {
divisionArray.clear();
divisionArray.add("Select Subject:-");
divisionArray.add("Science");
divisionArray.add("CTEVT");
divisionArray.notifyDataSetChanged();
}
答案 2 :(得分:0)
首先,两个微调器都使用相同的onItemSelected
。因此删除spinnerCountry.setOnItemSelectedListener(this);
和spinnerDivision.setOnItemSelectedListener(this);
在将数据设置为spinnerCountry之后,请使用它。
spinnerCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(i == 0){
fillMasterDivision();
}else{
fillBachelorDivision();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
并清除fillBachelorDivision()
答案 3 :(得分:0)
请参阅我的示例:
我有2个微调器, firstSpinner 和 secndSpinner ,第二个取决于First。
首先,您必须使用 onCreate 方法初始化微调框,然后执行以下步骤:
1-您将在第一个微调器中插入数据(“主”,“文凭”);
2-您将创建一个函数,该函数将使用关联的值更新第二个微调器。
firstSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
createSecondSpinnerByFirst(position);
}
@Override
public void onNothingSelected(AdapterView<?> adapter) {
}
});
private void createSecondSpinnerByFirst(Integer position) {
List<String> secValues = new ArrayList<>();
switch (position) {
case 1:
// insert in secValues array the strings of this condition
break;
case 2:
// insert in secValues array the strings of this condition
break;
}
// usually I use Adapter
final SpinnerAdapter adapter = new SpinnerAdapter(this, secValues);
secndSpinner.setAdapter(adapter);
}
//Code of myAdapter
public class SpinnerAdapter extends ArrayAdapter<String> {
private Context context;
private List<String> mValues;
public SpinnerAdapter(Context ctx, List<String> values) {
super(ctx, 0, values);
context = ctx;
mValues = values;
}
@Override
public int getCount() {
return mValues.size();
}
@Override
public Process getItem(int position) {
return mValues.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return returnLayoutSpinner(context, convertView, mValues.get(position));
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return returnLayoutSpinner(context, convertView, mValues.get(position));
}
}
//My function to return the adapter view
private View returnLayoutSpinner(Context context, View convertView, String value) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.spinner_items_view, null);
}
TextView txt = convertView.findViewById(R.id.txtSpinnerItem);
txt.setText(value, 25);
return convertView;
}
//spinner_items_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txtSpinnerItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|left"
android:padding="13dp"
android:textColor="@android:color/black"
android:textSize="16sp">
</TextView>
答案 4 :(得分:0)
private void fillDiplomaDivision() {
//add this line in your code and it solve
divisionArray.clear();
divisionArray.add("Select Subject:-");
divisionArray.add("Science");
divisionArray.add("CTEVT");
}