Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在spinner android中检查特定位置checbox_Android_Adapter_Android Spinner_Android Checkbox - Fatal编程技术网

如何在spinner android中检查特定位置checbox

如何在spinner android中检查特定位置checbox,android,adapter,android-spinner,android-checkbox,Android,Adapter,Android Spinner,Android Checkbox,我想要一个带有复选框的spinner(自定义adapterxml将在水平线上有一个图像、文本视图和复选框),我选中的任何复选框都应该被选中,即使在我重新打开应用程序时也是如此。那个特定的复选框状态应该被保存,当我再次打开应用程序时,那个特定的复选框应该在微调器中被选中,就是这样 说明: 我正在制作一个文本到语音的应用程序,当我点击spinner country name时,标志和一个复选框位于一条水平线上,几乎有50个国家使用自定义适配器,但我需要的是,每当用户单击任何国家/地区语言进行翻译时,

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

说明:

我正在制作一个文本到语音的应用程序,当我点击spinner country name时,标志和一个复选框位于一条水平线上,几乎有50个国家使用自定义适配器,但我需要的是,每当用户单击任何国家/地区语言进行翻译时,应用程序都应保存该复选框或特定位置。如果用户再次单击微调器,则用户应知道选择了哪种语言之前通过显示复选框选中,仅此而已。 谢谢期待您的回答

这就是我所拥有的,我想选中一个复选框


您可以在onsavedinstancestate中保存复选框的状态,并在oncreate方法中检索该状态。这仅用于短期使用,如果要保存复选框的状态以供将来应用程序使用,则应为每个复选框或SharedReference的状态使用数据库。短期来说,你可以用这样的东西 覆盖存储实例状态(超出状态:捆绑){ val复选框:复选框 outState.putBoolean(“checkbox\u x\u checked”,checkbox.isChecked) super.onSaveInstanceState(超出状态)

如果要保存数据以供长期使用,可以在选中数据后将其写入SharedReferences,例如类似这样的内容

    // 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();
}

微调器的CountryAdapter类

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);
}
公共类CountryAdapter扩展了BaseAdapter{
私有静态最终int视图类型=2;
私有静态最终整数类型_SELECTED=0;
私有静态最终整数类型\u未选择=1;
私人名单;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
私有CountryChangeListener CountryChangeListener;
公共CountryAdapter(上下文上下文,CountryChangeListener\u CountryChangeListener){
countryModelList=新的ArrayList();
layoutInflater=layoutInflater.from(上下文);
countryChangeListener=\u countryChangeListener;
}
公共作废添加(列表){
countryModelList.addAll(列表);
}
已选择公共同步无效设置国家(国际位置){
updateCountryListDta(位置);
如果(countryChangeListener!=null){
onCountryChangeListener.onCountryChange(getItem(position));
}
}
已选择公共同步作废设置国家(字符串已选择国家){
对于(CountryModel模型:countryModelList){
if(model.getName().equals(selectedCountry)){
选择的模型(真);
}否则{
选择的模型(真);
}
}
}
私有void updateCountryListDta(int位置){
对于(CountryModel模型:countryModelList){
所选型号(假);
}
getItem(位置).setSelected(真);
}
@凌驾
public int getCount(){
返回countryModelList.size();
}
@凌驾
public CountryModel getItem(内部位置){
返回countryModelList.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
public int getViewTypeCount(){
返回视图类型;
}
@凌驾
public int getItemViewType(int位置){
CountryModel model=getItem(位置);
返回模型.isSelected()?选择类型:未选择类型;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图=转换视图;
ViewHolder ViewHolder=null;
如果(视图==null){
视图=布局更平坦。充气(R.布局。国家/地区下拉菜单项,父项,false);
viewHolder=新的viewHolder(视图);
view.setTag(viewHolder);
}否则{
viewHolder=(viewHolder)view.getTag();
viewHolder.checkBox.setOnCheckedChangeListener(null);//删除以前连接的侦听器
}
CountryModel model=getItem(位置);
int type=getItemViewType(位置);
viewHolder.checkBox.setChecked(type==type\u SELECTED?true:false);
viewHolder.name.setText(model.getName());
viewHolder.checkBox.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener()){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
设置所选国家(职位);
}
});
返回视图;
}
私有类视窗持有者{
私有文本视图名称;
私有复选框;
视图保持器(视图){
name=view.findviewbyd(R.id.countryName);//替换为资源id
checkBox=view.findviewbyd(R.id.countryCheckBox);//替换为您的资源id
}
}
接口CountryChangeListener{
国家变更无效(CountryModel CountryModel);
}

}

但问题是所有复选框都将被选中,我不想选中我想选中特定位置复选框fox示例我选中了微调器中的第5项,因此当我重新打开应用程序时,微调器中的第5项复选框应该已经被选中让我知道我应该放在微调器中的条件以选中相同的复选框不是全部复选框将标记为选中,复选框的复选框应标记为选中,如果复选框没有数据,则不会标记为选中。当您选中一个复选框时,它会将数据写入共享首选项,该首选项将在重新打开时检索,这表示将此复选框标记为checkedwhere is storage position of specific checkobx?我们存储的是真还是假,当我们从SharedPreference获取数据时,如果SharedPreference中有真,那么它将标记所有复选框,因为没有位置,正如我所说的,如果我们选中第5位或第89位,位置的区别在哪里?谢谢你的帮助
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 + "]";
}
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);
}