为什么我的getTag,setTag,getSelected,setSelected复选框都没有给我Toast我正在寻找

时间:2018-01-21 18:17:21

标签: java android android-recyclerview boolean

对我而言,这似乎违背了逻辑,但......我知道它并不是因为它是我正在处理的计算机,而且我是一个人。

无论我的复选框是选中还是取消选中,我都会toast

'...clicked!'

如果我的checkbox未选中,我想要祝酒,'...unclicked!'

在我的模型中,SelectPhoneContact我有:

//this is for the checkbox
    //by default, make it unchecked
    boolean isSelected = false;

    public boolean getSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected){

            isSelected = selected;

    }

我正在使用recyclerView。在我的onBindViewHolder我有:

 @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        //bind the views into the ViewHolder
        //selectPhoneContact is an instance of the SelectPhoneContact class.
        //We will assign each row of the recyclerview to contain details of selectPhoneContact:

        //The number of rows will match the number of phone contacts
        final SelectPhoneContact selectPhoneContact = theContactsList.get(position);

        //if the row is a matching contact
        if (viewHolder.getItemViewType() == 1)

        {
            //in the title textbox in the row, put the corresponding name etc...
            ((MatchingContact) viewHolder).title.setText(selectPhoneContact.getName());
            ((MatchingContact) viewHolder).phone.setText(selectPhoneContact.getPhone());
            ((MatchingContact) viewHolder).check.setChecked(theContactsList.get(position).getSelected());
            ((MatchingContact) viewHolder).check.setTag(position);


            ((MatchingContact) viewHolder).check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //pos is the row number that the clicked checkbox exists in
                    Integer pos = (Integer) ((MatchingContact) viewHolder).check.getTag();

                    if (theContactsList.get(pos).isSelected=true)
                    {
                      //  theContactsList.get(pos).setSelected(true);
                        Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " clicked!", Toast.LENGTH_SHORT).show();

                    } else {
                        //theContactsList.get(pos).setSelected(false);
                        Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " unclicked!", Toast.LENGTH_SHORT).show();

                    }
                }
            });

2 个答案:

答案 0 :(得分:1)

if (theContactsList.get(pos).isSelected=true)中,您应该==或更好地写if (theContactsList.get(pos).isSelected)

答案 1 :(得分:0)

感谢Mike M的上述评论,这很有用:

 ((MatchingContact) viewHolder).check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           // ((MatchingContact) viewHolder).check.setOnClickListener(new CompoundButton.OnClickListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){

                    //pos is the row number that the clicked checkbox exists in
                    Integer pos = (Integer) ((MatchingContact) viewHolder).check.getTag();

                    //set the value of the checkbox accordingly onCheckedChange,
                    //to true or false
                    theContactsList.get(pos).setSelected(isChecked);

                    if(isChecked == true ) {
                        Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " clicked!", Toast.LENGTH_SHORT).show();

                    }

                    else {
                        Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " unclicked!", Toast.LENGTH_SHORT).show();

                    }
                    //Toast.makeText(context_type, theContactsList.get(pos).setSelected(isChecked), Toast.LENGTH_SHORT).show();


                }
            });