我在mainactivity中使用Switch按钮并使用CustonAdapter填充数据。我想要的是当我关闭我的开关按钮数据应该从我的SQL数据库以及自定义arraylist更改,因为我在自定义适配器中使用arraylist进行数据处理。 这是我的自定义适配器代码
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = (Integer) buttonView.getTag();
Object object = getItem(position);
DataModel dataModel = (DataModel) object;
Context context = buttonView.getContext();
if (isChecked){
MessageDbHelper mDbHelper = new MessageDbHelper(context);
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MessageEntry.COLUMN_STATUS_CHECK, MessageEntry.DAY_ON);
db.update(MessageEntry.TABLE_NAME,values,MessageEntry._ID + "=?",new String[]
{String.valueOf(buttonView.getTag())});
notifyDataSetChanged();
} else {
MessageDbHelper mDbHelper = new MessageDbHelper(context);
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MessageEntry.COLUMN_STATUS_CHECK, MessageEntry.DAY_OFF);
db.update(MessageEntry.TABLE_NAME,values,MessageEntry._ID + "=?",new String[]
{String.valueOf(buttonView.getTag())});
remove(dataModel); // here i was trying to change datamodel also remove is just for checking but not working
notifyDataSetChanged();
}
}
private static class ViewHolder {
TextView txtName;
TextView txtTime;
TextView txtRepeat;
Switch msgSwitch;
}
public CustomAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.fields, data);
this.dataSet = data;
this.mContext = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
DataModel dataModel = getItem(position);
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.fields, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
viewHolder.txtTime = (TextView) convertView.findViewById(R.id.txtTime);
viewHolder.txtRepeat = (TextView) convertView.findViewById(R.id.repeat);
viewHolder.msgSwitch = (Switch) convertView.findViewById(R.id.MessageSwitch);
result = convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
viewHolder.txtName.setText(dataModel.getPersonName());
viewHolder.txtTime.setText(dataModel.getTime());
viewHolder.txtRepeat.setText(dataModel.getRepeat());
viewHolder.msgSwitch.setChecked(dataModel.getStatus());
return convertView;
}
这是我的数据模型arraylist
public class DataModel implements Serializable{
String name;
String time;
String repeat;
boolean check;
public DataModel(String name, String time, String repeat, boolean check) {
this.name = name;
this.time = time;
this.repeat = repeat;
this.check = check;
}
public String getPersonName() {
return name;
}
public String getTime() {
return time;
}
public String getRepeat() {
return repeat;
}
public boolean getStatus() {
return check;
}}
答案 0 :(得分:0)