0

我需要应用程序中的设置,这些设置将在几个命名空间中可用(都在一个项目中)。我决定使用以下方法创建类:

    private SharedPreferences prefs;
private Context context;

public Preferenses(Context context){
    this.context=context;
}

public void invertLanguageState(String lang){
    prefs=context.getSharedPreferences(LEARN_WORDS_PREFS, Context.MODE_PRIVATE);
    boolean state=prefs.getBoolean(lang, false);
    Editor editor=prefs.edit();
    editor.putBoolean(lang,!state);
    editor.commit();
}

public void setLanguagesStates(String []langs,boolean []states){
    prefs=context.getSharedPreferences(LEARN_WORDS_PREFS, Context.MODE_PRIVATE);
    Editor editor=prefs.edit();
    final int size=states.length;
    for(int i=0;i<size;i++){
        editor.putBoolean(langs[i],states[i]);
    }
    editor.commit();
}
public boolean [] getLanguagesStates(String []keys){
    prefs=context.getSharedPreferences(LEARN_WORDS_PREFS,Context.MODE_PRIVATE);
    final int size=keys.length;
    boolean [] states=new boolean[size];
    for(int i=0;i<size;i++){
        states[i]=prefs.getBoolean(keys[i], false);
    }
    return states;
}

然后我尝试将它用于 ListActivity,我编写了我的适配器代码:

   public class UsableLanguagesAdapter extends ArrayAdapter<String>{

private final Context context;
private final String [] values;
private boolean [] states; // language states in checkbox (select or not)
private Preferenses prefs;


public UsableLanguagesAdapter(Context context,String []values) {

    super(context,RProvider.getLanguageUserItem(),values);      
    this.context=context;
    this.values=values;
    this.prefs=new Preferenses(context);    
    this.states=prefs.getLanguagesStates(values);
}   

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(RProvider.getLanguageUserItem(), parent, false);

    CheckBox checkBox=(CheckBox) rowView.findViewById(RProvider.getLanguageItemCheckBox());
    TextView textView = (TextView) rowView.findViewById(RProvider.getLanguageItemText());
    ImageView imageView = (ImageView) rowView.findViewById(RProvider.getLanguageItemImage());
    imageView.setImageResource(RProvider.getLangImageId(values[position]));
    textView.setText(values[position]);
    checkBox.setSelected(states[position]);     
    final int pos=position;
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
               Boolean state=prefs.getLangugeState(values[pos]);
               Log.d(state.toString(),"show state");
               prefs.invertLanguageState(values[pos]);
               state=prefs.getLangugeState(values[pos]);
               Log.d(state.toString(),"show state");
               ///checkBox.setSelected(isChecked);
           }            
    });
    return rowView;
}   

}

但是当我重新加载活动时,所有复选框都未选中。你能告诉我如何决定这个问题吗?这个结果在其他活动中也是必要的。

4

2 回答 2

1

如果我没有误解

checkBox.setSelected(states[position]); 

您需要设置checked复选框的属性。checkBox.setChecked(boolean)

来自文档:

setChecked(boolean)

更改视图的选中状态

于 2013-05-16T12:23:48.330 回答
0

问题是 checkBox.setSelected(states[position]); insteed checkBox.setChecked(states[position])

于 2013-05-16T17:57:48.890 回答