切换按钮切换单个按钮后,将为多个按钮激活OnCheckChangeListener,我的实现有什么问题?

时间:2018-07-31 21:10:32

标签: android android-recyclerview android-room android-togglebutton

我在回收站视图中制作了一个简单的切换按钮适配器,当按下按钮(切换为true)时,它将在Android房间数据库中创建一个条目。但是,由于某些原因,当我单击一个新按钮时,会切换其他多个按钮。在将按钮选中到控制台时,我尝试打印,并且可以确认当某个位置将按钮切换为true时,其他位置的另两个按钮也进行了切换。

insert函数本身在添加到数据库并切换按钮时似乎可以正常工作,但是会导致添加和切换其他按钮。

关于什么可能导致此问题的任何想法?

我的日志:

Log.d("Position", "" + position);

如您所见,在这种情况下,坠落已经被激活,然后我激活了gsw,效果很好,但是出于某种原因,然后激活了位置6和位置8按钮

我的适配器:

public class InjuryButtonAdapter2 extends RecyclerView.Adapter<InjuryButtonAdapter2.InjuryViewHolder> {

    private String[] injuries;
    private LayoutInflater inflater;
    Context context;
    private List<Injury> activeInjuries;
    private long patientId;

    public static class InjuryViewHolder extends RecyclerView.ViewHolder{
        ToggleButton injuryButton;

        public InjuryViewHolder(View itemView){
            super(itemView);

            injuryButton = itemView.findViewById(R.id.injury_toggle_button);
        }
    }


    public InjuryButtonAdapter2(Context context, long patientID, String[] injuries, List<Injury> activeInjuries) {
        this.injuries = injuries;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        this.patientId = patientID;
        this.activeInjuries = activeInjuries;
    }

    @Override
    public InjuryButtonAdapter2.InjuryViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.injury_button, parent, false);
        InjuryViewHolder holder = new InjuryViewHolder(v);

        return holder;
    }

    @Override
    public void onBindViewHolder(final InjuryButtonAdapter2.InjuryViewHolder holder, final int position) {
        holder.injuryButton.setText(injuries[position]);

        // Buttons always have same label
        holder.injuryButton.setTextOn(injuries[position]);
        holder.injuryButton.setTextOff(injuries[position]);

        // set all active injuries as checked
        for(Injury injury : activeInjuries){
            if(injury.getName().toString().toLowerCase().equals(injuries[position].toString().toLowerCase())){

                holder.injuryButton.setChecked(true);
                holder.injuryButton.setTextColor(Color.WHITE);
            }
        }

        holder.injuryButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (!editLocked) {
                    if (isChecked) {
                        // now enabled
                        // add injury

                        Log.d("Position", "" + position);
                        MainActivity.injuryViewModel.insert(new Injury(patientId, injuries[position].toLowerCase(),70));

                        if (MainActivity.darkMode != true) {
                            holder.injuryButton.setTextColor(Color.WHITE);
                        }
                    } else {
                        // now disabled
                        // remove injury

                        MainActivity.injuryViewModel.delete(injuries[position].toLowerCase(), patientId);

                        if (MainActivity.darkMode != true) {
                            holder.injuryButton.setTextColor(context.getResources().getColor(R.color.primary_text_default_material_light));
                        }
                    }
                } else {
                    // if locked, then toggle back (don't let user modify injury selection)
                    holder.injuryButton.toggle();
                }
            }
        });
    }

    @Override
    public int getItemCount(){
        return injuries.length;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView view){
        super.onAttachedToRecyclerView(view);
    }

    public void updateList(List<Injury> list){
        this.activeInjuries = list;
        notifyDataSetChanged();
    }
}

0 个答案:

没有答案