Android将所有复选框设置为来自另一个活动

时间:2018-04-04 18:11:16

标签: android checkbox android-recyclerview

我有一个Start.javaCandidateAdapter.java类活动,他们会延伸RecyclerView.Adapter<CandidateAdapter.MyViewHolder>

CandidateAdapter.java上,我有一个名称列表,每个名称都有一个复选框。

Start.java按下“保存”按钮时,我将LocalBroadcast值“false”发送到CandidateAdapter.java,以便在将每个复选框的状态保存到数据库后可以取消选中所有复选框。

我用onBindViewHolder CandidateAdapter.java方法写了这个代码来实现这个目标:

if (!checkBoxStatus) holder.voteCheck.setChecked(false);

但它不起作用。代码holder.voteCheck.setChecked(false)未执行。也许是因为onBindViewHolder方法在加载一次并且与听众一起执行时我认为......

是否可以将所有复选框设置为uncheked状态?如果有必要,我可以发送我的代码。

enter code here
        public CandidateAdapter(Context c){
    inflater = LayoutInflater.from(c);
    mContext=c;
}
public CandidateAdapter(Context mContext,List<CandidateModel> candidateList){
    this.mContext = mContext;
    this.candidateList=candidateList;
}

public class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView name;
    public CheckBox voteCheck;
    public MyViewHolder(View view){
        super(view);
        name = view.findViewById(R.id.nameCandidate);
        voteCheck =  view.findViewById(R.id.addVote);
        //This command to prevent the Recycler view from recycling it?s content
        this.setIsRecyclable(false);
    }
}

@Override
public CandidateAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


                    rowView = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.row_list_item,parent,false);

    return new MyViewHolder(rowView);
     }
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final CandidateAdapter.MyViewHolder holder, final int position) {
    //Create instance of Database Helper
    DatabaseHelper mDbHelper = new DatabaseHelper(mContext);
    //Set database to write mode
    db = mDbHelper.getWritableDatabase();
    //Create a ContentValue object with columns names to be the keys
    final ContentValues values = new ContentValues();

        final CandidateModel candidateModel = candidateList.get(position);

        LocalBroadcastManager.getInstance((mContext)).registerReceiver(mMsgReciever,
                new IntentFilter("count_voters"));
        LocalBroadcastManager.getInstance((mContext)).registerReceiver(messageFromStartVoting,
                new IntentFilter("custom"));
        //Read clear message from StartVoting.java class
        BroadcastReceiver clearMessage = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                checkBoxStatus = intent.getBooleanExtra("set_check_boxes_to_false",false);
                Log.i("Receiver","Got message "+checkBoxStatus);
            }
        };
        LocalBroadcastManager.getInstance((mContext)).registerReceiver(clearMessage,
                new IntentFilter("customClearCheckboxes"));
        createCheckedHolder();

if(!checkBoxStatus)  holder.voteCheck.setChecked(假);

        holder.name.setText(candidateModel.getCandidateName());
        holder.voteCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                checkedHolder[holder.getAdapterPosition()] = isChecked;
                int newposition=holder.getAdapterPosition()+1;
                if (checkedHolder[position] ) {
                    //Store votes for each voter to  2D boolean array
                  //  countVotes(votesOfCandidates, position, countFromStartVoting, isChecked, vtsOfCandidates, mContext);
                    switch (countFromStartVoting+1){
                        case 1:
                            values.put(DataContract.VoteEntry.COLUMN_VOTE_VOTER1,1);
                            //Update an existing row with data to database table and return a rowId
                            db.update(DataContract.VoteEntry.TABLE_NAME_VOTE, values, "_id="+newposition, null);
                            break;
                        case 2:
                            values.put(DataContract.VoteEntry.COLUMN_VOTE_VOTER2,1);
                            //Update an existing row with data to database table and return a rowId
                            db.update(DataContract.VoteEntry.TABLE_NAME_VOTE, values, "_id="+newposition, null);
                            break;
                        case 3:
                            values.put(DataContract.VoteEntry.COLUMN_VOTE_VOTER3,1);
                            //Update an existing row with data to database table and return a rowId
                            db.update(DataContract.VoteEntry.TABLE_NAME_VOTE, values, "_id="+newposition, null);
                            break;

                    }
                    //Count the clicked checkboxes +1
                    updateCheckedVotes(++counter);
                }
                else {
                  //  countVotes(votesOfCandidates, position, countFromStartVoting, isChecked, vtsOfCandidates, mContext);
                    switch (countFromStartVoting+1){
                        case 1:
                            values.put(DataContract.VoteEntry.COLUMN_VOTE_VOTER1,0);
                            //Update an existing row with data to database table and return a rowId
                            db.update(DataContract.VoteEntry.TABLE_NAME_VOTE, values, "_id="+newposition, null);
                            break;
                        case 2:
                            values.put(DataContract.VoteEntry.COLUMN_VOTE_VOTER2,0);
                            //Update an existing row with data to database table and return a rowId
                            db.update(DataContract.VoteEntry.TABLE_NAME_VOTE, values, "_id="+newposition, null);
                            break;
                        case 3:
                            values.put(DataContract.VoteEntry.COLUMN_VOTE_VOTER3,0);
                            //Update an existing row with data to database table and return a rowId
                            db.update(DataContract.VoteEntry.TABLE_NAME_VOTE, values, "_id="+newposition, null);
                            break;


                    }
                    //Count the clicked checkboxes -1
                    updateCheckedVotes(--counter);
               }
                Log.i(TAG, candidateModel.getCandidateName() + String.valueOf(position) + " " + isChecked +"??????? "+countFromStartVoting + " totalChecked boxes" + counter);
               // countFinalVotes(signal);
            }
        });
    } //end if statement
}
private void createCheckedHolder(){
    checkedHolder = new boolean[getItemCount()];
}
//Show the list of candidates
@Override
public int getItemCount() {
    return candidateList.size();
}
// I send with Broadcast to StartVoting.java class the checked and unchecked checkboxes
public void updateCheckedVotes(int mCounter){
    Intent intent = new Intent("update_text");
    intent.putExtra("checkedItems",mCounter);
    LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
}

public BroadcastReceiver mMsgReciever = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        countFromStartVoting = intent.getIntExtra("count",1);
    }
};
public BroadcastReceiver messageFromStartVoting = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        signal = intent.getBooleanExtra("endOfVoting",false);
    }
};

}

1 个答案:

答案 0 :(得分:0)

您应该调用notifyDataSetChanged()来刷新整个Recyclerview或notifyItemChanged(int position)以刷新特定项目以在回收站视图中刷新视图。

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyDataSetChanged()

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyItemChanged(int)