经过几个小时的搜索,我无法完全理解这一点。
我尝试使用共享偏好设置保存复选框状态。据我所知,它拯救了国家。但是,这个位置全部都是关闭的,我不确定我在这里做错了什么。 btnToggle状态工作正常,但我没有存储在数据库中的checkBox状态不是,我不想使用数据库来存储该checkBox的状态。任何帮助将不胜感激!
public class AlarmListAdapter extends BaseAdapter {
private Context mContext;
private List<AlarmModel> mAlarms;
Alarms displayAlarm = new Alarms();
ViewHolder viewHolder;
private CheckBox checkBox ;
public AlarmListAdapter(Context context, List<AlarmModel> alarms) {
mContext = context;
mAlarms = alarms;
}
public void setAlarms(List<AlarmModel> alarms) {
mAlarms = alarms;
}
@Override
public int getCount() {
if (mAlarms != null) {
return mAlarms.size();
}
return 0;
}
@Override
public Object getItem(int position) {
if (mAlarms != null) {
return mAlarms.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
if (mAlarms != null) {
return mAlarms.get(position).id;
}
return 0;
}
private class ViewHolder
{
RelativeLayout share;
RelativeLayout fav;
RelativeLayout info;
CheckBox checkBox;
Switch btnToggle;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final AlarmModel model = (AlarmModel) getItem(position);
if(convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.alarm_list_item, null);
viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.visible_toggle);
viewHolder.btnToggle = (Switch) convertView.findViewById(R.id.alarm_item_toggle);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.checkBox.getTag(position);
viewHolder.btnToggle.getTag(position);
}
viewHolder.checkBox.setTag(Long.valueOf(model.id)); // This line is important.
viewHolder.btnToggle.setTag(Long.valueOf(model.id));
TextView txtTime = (TextView) convertView.findViewById(R.id.alarm_item_time);
txtTime.setText(String.format("%02d:%02d", model.timeHour, model.timeMinute));
TextView txtName = (TextView) convertView.findViewById(R.id.alarm_item_name);
txtName.setText(model.name);
updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_sunday), model.getRepeatingDay(AlarmModel.SUNDAY));
updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_monday), model.getRepeatingDay(AlarmModel.MONDAY));
updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_tuesday), model.getRepeatingDay(AlarmModel.TUESDAY));
updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_wednesday), model.getRepeatingDay(AlarmModel.WEDNESDAY));
updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_thursday), model.getRepeatingDay(AlarmModel.THURSDAY));
updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_friday), model.getRepeatingDay(AlarmModel.FRDIAY));
updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_saturday), model.getRepeatingDay(AlarmModel.SATURDAY));
viewHolder.btnToggle.setChecked(model.isEnabled);
viewHolder.btnToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
((AlarmSettings) mContext).setAlarmEnabled(((Long) buttonView.getTag()).longValue(), isChecked);
}
});
viewHolder.checkBox.setChecked(getFromSP("visbox"));
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isVisible){
viewHolder.checkBox.getTag(position);
try {
saveInSp("visbox", isVisible);
} catch (Exception e) {
e.printStackTrace();
}
if(isVisible) {
// Do stuff here
}
else {
}
}
});
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((AlarmSettings) mContext).startAlarmDetailsActivity(((Long) view.getTag()).longValue());
}
});
convertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
((AlarmSettings) mContext).deleteAlarm((Long) view.getTag());
return true;
}
});
return convertView;
}
private void updateTextColor(TextView view, boolean isOn) {
if (isOn) {
view.setTextColor(mContext.getResources().getColor(R.color.primary_dark));
} else {
view.setTextColor(Color.BLACK);
}
}
private boolean getFromSP(String key){
SharedPreferences preferences = mContext.getSharedPreferences("epimelis.com.lyre", android.content.Context.MODE_PRIVATE);
boolean val = preferences.getBoolean(key, false);
System.out.println("getting key: "+ key + " with value: " + val);
return val;
}
private void saveInSp(String key,boolean value) throws Exception {
//if(!value) throw new Exception("Die Die Die");ooo
System.out.println("Saving key: " + key + " with value: " + value);
SharedPreferences preferences = mContext.getSharedPreferences("epimelis.com.lyre", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
}
答案 0 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
尝试再添加一个标记&#34; isChecked&#34;在您的AlarmModel类中,默认值为false,并使用下面的代码。
public class AlarmListAdapter extends BaseAdapter {
private Context mContext;
private List<AlarmModel> mAlarms;
Alarms displayAlarm = new Alarms();
public AlarmListAdapter(Context context, List<AlarmModel> alarms) {
mContext = context;
mAlarms = alarms;
}
public void setAlarms(List<AlarmModel> alarms) {
mAlarms = alarms;
}
@Override
public int getCount() {
return mAlarms.size();
}
@Override
public Object getItem(int position) {
return mAlarms.get(position);
}
@Override
public long getItemId(int position) {
return mAlarms.get(position).id;
}
static class ViewHolder
{
CheckBox checkBox;
Switch btnToggle;
TextView txtTime;
TextView txtName;
TextView txtSunday;
TextView txtMonday;
TextView txtTuesday;
TextView txtWednesday;
TextView txtThursday;
TextView txtFriday;
TextView txtSaturday;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.alarm_list_item, null);
viewHolder.btnToggle = (Switch) convertView.findViewById(R.id.alarm_item_toggle);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.visible_toggle);
viewHolder.txtTime = (CheckBox) convertView.findViewById(R.id.alarm_item_time);
viewHolder.txtName = (CheckBox) convertView.findViewById(R.id.alarm_item_name);
viewHolder.txtSunday = (CheckBox) convertView.findViewById(R.id.alarm_item_sunday);
viewHolder.txtMonday = (CheckBox) convertView.findViewById(R.id.alarm_item_monday);
viewHolder.txtTuesday = (CheckBox) convertView.findViewById(R.id.alarm_item_tuesday);
viewHolder.txtWednesday = (CheckBox) convertView.findViewById(R.id.alarm_item_wednesday);
viewHolder.txtThursday = (CheckBox) convertView.findViewById(R.id.alarm_item_thursday);
viewHolder.txtFriday = (CheckBox) convertView.findViewById(R.id.alarm_item_friday);
viewHolder.txtSaturday = (CheckBox) convertView.findViewById(R.id.alarm_item_saturday);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtTime.setText(String.format("%02d:%02d", mAlarms.get(position).timeHour, mAlarms.get(position).timeMinute));
viewHolder.txtName.setText(mAlarms.get(position).name);
updateTextColor(viewHolder.txtMonday, mAlarms.get(position).getRepeatingDay(AlarmModel.SUNDAY));
updateTextColor(viewHolder.txtMonday, mAlarms.get(position).getRepeatingDay(AlarmModel.MONDAY));
updateTextColor(viewHolder.txtMonday, mAlarms.get(position).getRepeatingDay(AlarmModel.TUESDAY));
updateTextColor(viewHolder.txtMonday, mAlarms.get(position).getRepeatingDay(AlarmModel.WEDNESDAY));
updateTextColor(viewHolder.txtMonday, mAlarms.get(position).getRepeatingDay(AlarmModel.THURSDAY));
updateTextColor(viewHolder.txtMonday, mAlarms.get(position).getRepeatingDay(AlarmModel.FRDIAY));
updateTextColor(viewHolder.txtMonday, mAlarms.get(position).getRepeatingDay(AlarmModel.SATURDAY));
viewHolder.btnToggle.setChecked(mAlarms.get(position).isEnabled);
viewHolder.btnToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
((AlarmSettings) mContext).setAlarmEnabled((position), isChecked);
mAlarms.get(position).isEnabled = isChecked;
}
});
viewHolder.checkBox.setChecked(mAlarms.get(position).isChecked);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isVisible){
mAlarms.get(position).isChecked = isVisible;
if(isVisible) {
// Do visible stuff here
}
else {
// Do invisible or gone stuff here
}
}
});
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((AlarmSettings) mContext).startAlarmDetailsActivity(position);
}
});
convertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
((AlarmSettings) mContext).deleteAlarm(position);
return true;
}
});
return convertView;
}
private void updateTextColor(TextView view, boolean isOn) {
if (isOn) {
view.setTextColor(mContext.getResources().getColor(R.color.primary_dark));
} else {
view.setTextColor(Color.BLACK);
}
}
}