如何检查微调器中的特定位置复选框 android

How to checked specific position checbox in spinner android

我想要一个带复选框的 spinner(自定义 adapter xml 将在水平线上有一个图像、文本视图和复选框)并且我选中的任何复选框都应该被选中,即使我重新打开应用程序。那个特定的复选框状态应该被保存,当我再次打开应用程序时,那个特定的复选框应该在微调器中被选中。

描述:

我正在制作一个文本到语音应用程序,当我点击微调国家名称时,旗帜和一个复选框在一条水平线上,几乎有 50 个国家使用自定义适配器,但我需要的是每当用户点击任何国家翻译语言应用程序应保存该复选框或该特定位置,如果用户再次单击微调器,用户应该知道之前选择了哪种语言,只需显示复选框即可。 感谢期待您的回答

这就是我所拥有的,并希望将一个复选框标记为已选中

您可以将复选框的状态保存在 onsavedinstancestate 中,并在 oncreate 方法中检索该状态。这仅供短期使用,如果您想保存复选框的状态以供将来使用应用程序,则应使用数据库来存储每个复选框或共享首选项的状态。对于短期你可以使用这样的东西 覆盖乐趣 onSaveInstanceState(outState: Bundle) { 验证复选框:复选框 outState.putBoolean("checkbox_x_checked",checkBox.isChecked) super.onSaveInstanceState(outState)

}

override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
    super.onCreate(savedInstanceState, persistentState)
    val checkBox : CheckBox
    //assign all Views and Ids 
    if (savedInstanceState!= null){
        checkBox.isChecked = savedInstanceState.getBoolean("checkbox_x_checked",false)
    }
}

如果您想保存数据以供长期使用,您可以将选中的数据写入 sharedpreferences 例如这样的东西

    // get shared preferences
    val sharedpref  = getSharedPreferences("checked_checkboxes", Context.MODE_PRIVATE)
    val sharedpref_editor = sharedpref.edit()
    //assign checkboxes
    val checkBox_x : CheckBox

    //set checkbox old value
    checkbox_x.isChecked = sharedpref.getBoolean("checkbox_x_checked",false)

    //assign oncheckedchangelistener for the checkboxes
    checkBox_x.setOnCheckedChangeListener { buttonView, isChecked ->
        sharedpref_editor.putBoolean("checkbox_x_checked",isChecked)
        sharedpref_editor.apply()
    }

试试这个

//这是从activity/fragment

添加适配器的方法
    CountryAdapter adapter = new CountryAdapter(getApplicationContext(), new CountryAdapter.CountryChangeListener() {
        @Override
        public void onCountryChange(CountryModel countryModel) {
            //persist selected country name in preference
        }
    });

    List<CountryModel> list = loadAllCountries();
    adapter.add(list);
    String selectedCountry = getSelectedCountry();
    if(!TextUtils.isEmpty(selectedCountry)){
        adapter.setCountrySelected(selectedCountry);
    }
    adapter.notifyDataSetChanged();

CountryModel 是您的适配器的型号 class。

public class CountryModel {

private String name;

private Drawable countryFlagIcon; //it could be bitmap or resource Id of the icon

/*
    isSelected would be true when user selects that particular Country
*/

private boolean isSelected;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Drawable getCountryFlagIcon() {
    return countryFlagIcon;
}

public void setCountryFlagIcon(Drawable countryFlagIcon) {
    this.countryFlagIcon = countryFlagIcon;
}

public boolean isSelected() {
    return isSelected;
}

public CountryModel(String _name,Drawable _countryFlagIcon){
    name = _name;
    countryFlagIcon = _countryFlagIcon;
}

public CountryModel(String _name,Drawable _countryFlagIcon,boolean _isSelected){
    this(_name,_countryFlagIcon);
    isSelected = _isSelected;
}

public void setSelected(boolean _isSelected){
    isSelected = _isSelected;
}

@Override
public int hashCode() {
    int result = 1;

    result = 31 * result + (name == null ? 0 : name.hashCode());

    result = 31 * result + Boolean.valueOf(isSelected).hashCode();

    return result;
}

@Override
public boolean equals(Object obj) {
    if(obj == null || obj.getClass() != getClass()) return false;

    if(this == obj) return true;

    CountryModel model = (CountryModel)obj;

    return name != null && name.equals(model.name) && isSelected == model.isSelected;
}


@Override
public String toString() {
    return "[country = " + name + " isSelected = " + isSelected + "]";
}

}

适用于 Spinner

的 CountryAdapter 适配器 class
public class CountryAdapter extends BaseAdapter {

private static final int VIEW_TYPES = 2;

private static final int TYPE_SELECTED = 0;

private static final int TYPE_UNSELECTED = 1;

private List<CountryModel> countryModelList;

private LayoutInflater layoutInflater;

private CountryChangeListener countryChangeListener;

public CountryAdapter(Context context,CountryChangeListener _countryChangeListener){
    countryModelList = new ArrayList<>();
    layoutInflater = LayoutInflater.from(context);
    countryChangeListener = _countryChangeListener;
}

public void add(List<CountryModel> list){
    countryModelList.addAll(list);
}

public synchronized void setCountrySelected(int position) {
    updateCountryListDta(position);
    if(countryChangeListener != null){
        countryChangeListener.onCountryChange(getItem(position));
    }
}

public synchronized void setCountrySelected(String selectedCountry) {
    for(CountryModel model:countryModelList){
        if(model.getName().equals(selectedCountry)){
            model.setSelected(true);
        }else{
            model.setSelected(true);
        }
    }
}

private void updateCountryListDta(int position) {
    for(CountryModel model:countryModelList){
        model.setSelected(false);
    }
    getItem(position).setSelected(true);
}



@Override
public int getCount() {
    return countryModelList.size();
}

@Override
public CountryModel getItem(int position) {
    return countryModelList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public int getViewTypeCount() {
    return VIEW_TYPES;
}

@Override
public int getItemViewType(int position) {
    CountryModel model = getItem(position);
    return model.isSelected() ? TYPE_SELECTED : TYPE_UNSELECTED;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder viewHolder = null;
    if(view == null ){
        view = layoutInflater.inflate(R.layout.country_dropdown_item,parent,false);
        viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) view.getTag();
        viewHolder.checkBox.setOnCheckedChangeListener(null);//remove previous Attached listener
    }

    CountryModel model = getItem(position);

    int type = getItemViewType(position);

    viewHolder.checkBox.setChecked(type == TYPE_SELECTED ? true:false);

    viewHolder.name.setText(model.getName());

    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            setCountrySelected(position);
        }
    });
    return view;
}



private class ViewHolder{
    private TextView name;

    private CheckBox checkBox;

    ViewHolder(View view){
        name = view.findViewById(R.id.countryName);//replace with your resource ids
        checkBox = view.findViewById(R.id.countryCheckBox);//replace with your resource ids
    }
}

interface CountryChangeListener{
    void onCountryChange(CountryModel countryModel);
}

}