微调器项目取决于另一个微调器的选定项目

时间:2018-10-08 17:39:57

标签: java android spinner

我有两个Custom Spinners,并且必须从第一个的选定项目中修改第二个Spinner的可用项目。例如:如果我在第一个微调器中选择值“ Android”,则在第二个微调器中,我想查看Android版本列表。如果选择“ Apple”,我想查看iOS版本。对于许多其他操作系统,依此类推。如果需要的话,我也可以选择其中的两个或三个,在第二个Spinner中,我要查看在第一个Spinner中选择的每个操作系统的所有操作系统版本。

到这里都没问题。

问题在于自定义微调框同时具有TextView和Checkbox。我想,这就是让我麻烦的原因。 结果是我可以选中/取消选中某项,但是在第二个Spinner中我什么也看不到。

在第一个微调器中取消选中某个操作系统时,我尚未实现删除操作系统版本的机制,但是我想我可以通过removeAll()来做到这一点

我在下面附上代码。

(对不起,我的英语不好。。。我也在努力改善:') OT,您能为我推荐一本适合初学者的好书吗? 非常感谢

  

MainActivity.java

    final String[] select_SO = {
            "Select SO", "Android", "iOS", "Microsoft Phone"};
    final Spinner spinner2 = findViewById(R.id.spinner2);

    final ArrayList<stateVO> listSO = new ArrayList<>();
    for (int i = 0; i < select_SO.length; i++) {
        stateVO stateVO = new stateVO();
        stateVO.setTitle(select_SO[i]);
        stateVO.setSelected(false);
        listSO.add(stateVO);
    }
    MyAdapter myAdapter2 = new MyAdapter(semplice.this, 0,
            listSO);
    spinner2.setAdapter(myAdapter2);

        //spinner with OS VERSION
    final ArrayList<String> select_version = new ArrayList<String>();
    select_version.add("First select OS");
    final String[] apple = {"iPhone OS", "iPhone OS 2", "iPhone OS 3", "iOS 4",
            "iOS 5", "iOS 6", "iOS 7", "iOS 8", "iOS 9", "iOS 10", "iOS 11",
            "iOS 12"};
    final String[] android = {"Android 1.5", "Android 1.6", "Android 2.0 / 2.1",
            "Android 2.2", "Android 2.3", "Android 3.0", "Android 4.0",
            "Android 4.1 / 4.2 / 4.3 ", "Android 4.4", "Android 5.0 / 5.1",
            "Android 6.0", "Android 7.0 / 7.1", "Android 8.0 / 8.1",
            "Android 9.0"};
    final String[] microsoft = {"Windows Phone 7.0", "Windows Phone 7.5 Mango",
            "Windows Phone 7.5 Tango", "Windows Phone 7.8",
            "Windows Phone 8.0", "Windows Phone 8.1", "Windows Phone 10"};
    final Spinner spinner3 = findViewById(R.id.spinner3);
    final ArrayList<stateVO> listVersions = new ArrayList<>();
    MyAdapter myAdapter3 = new MyAdapter(semplice.this, 0, listVersions);
    spinner3.setAdapter(myAdapter3);

    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
{
        int option;
        @Override
        public void onItemSelected(AdapterView<?> myAdapter, View view,
                                   int position, long id) {
            option=position;
            if (position == 1) select_version.addAll(Arrays.asList(android));
            if (position == 2) select_version.addAll(Arrays.asList(apple));
            if (position == 3) select_version.addAll(Arrays.asList(microsoft));
            for (int i = 0; i < select_version.size(); i++) {
                stateVO stateVO = new stateVO();
                stateVO.setTitle(select_version.get(i));
                stateVO.setSelected(false);
                listVersions.add(stateVO);
            }
            MyAdapter myAdapter3 = new MyAdapter(semplice.this, 0,
                    listVersions);
            spinner3.setAdapter(myAdapter3);
        }

        @Override
        public void onNothingSelected(AdapterView<?> myAdapter) {

            stateVO stateVO = new stateVO();
            stateVO.setTitle("First Select SO");
            stateVO.setSelected(false);
            listVersions.add(stateVO);
            MyAdapter myAdapter3 = new MyAdapter(semplice.this, 0,
                    listVersions);
            spinner3.setAdapter(myAdapter3);

        }
    });
  

MyAdapter.java

public class MyAdapter extends ArrayAdapter<stateVO> {
private Context mContext;
private ArrayList<stateVO> listState;
private MyAdapter myAdapter;
private boolean isFromView = false;

public MyAdapter(Context context, int resource, List<stateVO> objects) {
    super(context, resource, objects);
    this.mContext = context;
    this.listState = (ArrayList<stateVO>) objects;
    this.myAdapter = this;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

public View getCustomView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        LayoutInflater layoutInflator = LayoutInflater.from(mContext);
        convertView = layoutInflator.inflate(R.layout.spinner_item, null);
        holder = new ViewHolder();
        holder.mTextView = (TextView) convertView
                .findViewById(R.id.text);
        holder.mCheckBox = (CheckBox) convertView
                .findViewById(R.id.checkbox);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.mTextView.setText(listState.get(position).getTitle());

    // To check weather checked event fire from getview() or user input
    isFromView = true;
    holder.mCheckBox.setChecked(listState.get(position).isSelected());
    isFromView = false;

    if ((position == 0)) {
        holder.mCheckBox.setVisibility(View.INVISIBLE);
    } else {
        holder.mCheckBox.setVisibility(View.VISIBLE);
    }
    holder.mCheckBox.setTag(position);
    holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int getPosition = (Integer) buttonView.getTag();

            if (!isFromView) {
                listState.get(position).setSelected(isChecked);
            }
        }
    });

    return convertView;
}

private class ViewHolder {
    private TextView mTextView;
    private CheckBox mCheckBox;
}
}

0 个答案:

没有答案