我有一个对话框,我想添加一个recyclerView或listView,我知道我必须使用List来控制每行中的项目。现在,当我想要更改复选框或radioButton的值时,List中的所有数据都在变化。我使用了Log,我得到了所有布尔数据同时发生了变化。我将所有项目移动到一个活动但问题仍然存在。似乎arrayList数据之间存在链接。
此外,我更改了代码并使用了SwitchCompat,但是当我想要更改其中一个时,mDataSet中的所有布尔数据都在变化。
public class AdapterSetOnOffTime extends RecyclerView.Adapter<AdapterSetOnOffTime.ViewHolder> {
private Context mContext;
private ArrayList<StructSetOnOffTime> mDataSet;
private View mView;
public AdapterSetOnOffTime(Context context, ArrayList<StructSetOnOffTime> dataSet) {
mContext = context;
mDataSet = dataSet;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int id = R.layout.layout_list_settime_items;
mView = LayoutInflater.from(mContext).inflate(id, parent, false);
return new ViewHolder(mView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mSwitch.setChecked(mDataSet.get(position).setOnOffValue);
holder.mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
mDataSet.get(position).setOnOffValue = true;
}else{
mDataSet.get(position).setOnOffValue = false;
}
}
});
}
@Override
public int getItemCount() {
return mDataSet.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextTitle;
public TextView mTextValue;
public ImageButton mBtn;
public SwitchCompat mSwitch;
public ViewHolder(View itemView) {
super(itemView);
mTextTitle = (TextView) itemView.findViewById(R.id.txtSetTime);
mTextValue = (TextView) itemView.findViewById(R.id.txtOnOffStatus);
mBtn = (ImageButton) itemView.findViewById(R.id.btnSetTime);
mSwitch = (SwitchCompat) itemView.findViewById(R.id.swPrefSwitch);
}
}
}
更新回答
浪费了一天之后,我意识到我在填写ArrayList时犯了一个小错误,我将分享它。
填充适配器的上一个方法:
StructSetOnOffTime item = new StructSetOnOffTime();
ArrayList<StructSetOnOffTime> arrayList = new ArrayList<>();
for(int i = 0; i < 9; i++){
item.setTimeValue = null;
item.setOnOffValue = false;
arrayList.add(item);
}
正如@Anatoli所说,我在本节中遇到了问题,我通过将上层方法更改为此方法来解决它:
ArrayList<StructSetOnOffTime> arrayList = new ArrayList<>();
for(int i = 0; i < 9; i++){
StructSetOnOffTime item = new StructSetOnOffTime();
item.setTimeValue = null;
item.setOnOffValue = false;
arrayList.add(item);
}
答案 0 :(得分:1)
您设置为radOn
和radOff
相同OnCheckedChangeListener
并检入侦听器是否设置为true
。你为什么不做两个听众?
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mDataSet.get(position).onOffStatus = true;
// update adapter
notifyDataSetChanged();
}
}
};
holder.radOn.setOnCheckedChangeListener(listener);
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mDataSet.get(position).onOffStatus = false;
// update adapter
notifyDataSetChanged();
}
}
};
holder.radOff.setOnCheckedChangeListener(listener);
看起来更简单
调用
holder.radOn.setChecked(holder.radOn == buttonView);
holder.radOff.setChecked(holder.radOff == buttonView);
触发侦听器。要更新列表中的值,只需拨打adapter.notifyDataSetChanged();
<强>更新强>
对于列表中的每个新项目,您必须初始化新的Holder
RecyclerView
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
// Inflate the custom layout
View itemView = LayoutInflater.from(parent.getContext())
.inflate(layoutId, parent, false);
return new Holder(itemView, viewType);
}
ArayList
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
Holder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new Holder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (Holder) convertView.getTag();
}
Item item = getItem(position);
fill(item, position);
return convertView;
}
答案 1 :(得分:1)
当您设置视图的检查状态时(如果视图没有被破坏,也会从您的代码中设置),您的检查侦听器将被调用,因此您需要手动维护状态。它适用于所有单选按钮,复选框等。
holder.mSwitch.setOnCheckedChangeListener(null);
holder.mSwitch.setChecked(mDataSet.get(position).setOnOffValue);
holder.mSwitch.setOnClickListener(new OnClickListener(){
//holder.mSwitch.setCheck(!mDataSet.get(position).setOnOffValue);
mDataSet.get(position).setOnOffValue = !mDataSet.get(position).setOnOffValue;
});