简体   繁体   中英

Android listview with checkbox issue

If I click the checkbox it means it will be unchecked during scrolling and vice versa. Also I want to refresh the home fragment after clicking the checkbox. Anyone give the solution to fix these two issues?

customViewHolder.checkBox.setChecked(feedItem.isSelected());

customViewHolder.checkBox.setTag(position);

customViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //set your object's last status
        //feedItem.setSelected(isChecked);
        //customViewHolder.checkBox.setTag(position);
        int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
        feedItemList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
    }
}); 

if(!feedItem.getLikedStatus().isEmpty()) {
    if(feedItem.getLikedStatus().matches("liked")) {
        customViewHolder.checkBox.setChecked(true);
    } else {
        customViewHolder.checkBox.setChecked(false);
    }
}

//set Like Count
if(!feedItem.getLikedCount().isEmpty()) {
    if(feedItem.getLikedCount().matches("1")) {
        customViewHolder.likedUserName.setVisibility(View.VISIBLE);
        customViewHolder.likedUserName.setText(feedItem.getLikedUserName());
        customViewHolder.likeImageView.setVisibility(View.VISIBLE);
    } else if(feedItem.getLikedCount().matches("0")) {
        customViewHolder.likedUserName.setVisibility(View.GONE);
        customViewHolder.likeImageView.setVisibility(View.GONE);
    } else {
        customViewHolder.likedUserName.setVisibility(View.VISIBLE);
        customViewHolder.likedUserName.setText(feedItem.getLikedCount()+" likes");
        customViewHolder.likeImageView.setVisibility(View.VISIBLE);
    }
}

customViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(!feedItem.getLikedCount().isEmpty()) {
            if(feedItem.getLikedCount().matches("0")) {
                if(customViewHolder.checkBox.isChecked()) {
                    customViewHolder.likeImageView.setVisibility(View.VISIBLE);
                    customViewHolder.likedUserName.setVisibility(View.VISIBLE);
                    customViewHolder.likedUserName.setText(userName);
                    //call like url
                    postLikeUnlike(feedItem.getPostID(),like);
                } else {
                    //call unlike url
                    customViewHolder.likeImageView.setVisibility(View.GONE);
                    customViewHolder.likedUserName.setVisibility(View.GONE);
                    postLikeUnlike(feedItem.getPostID(), unlike);
                }
            } else {
                if(customViewHolder.likedUserName.getText().toString().matches(userName)) {
                    if (customViewHolder.checkBox.isChecked()) {
                        customViewHolder.likeImageView.setVisibility(View.VISIBLE);
                        customViewHolder.likedUserName.setVisibility(View.VISIBLE);
                        customViewHolder.likedUserName.setText(userName);
                        //call like url
                        postLikeUnlike(feedItem.getPostID(),like);
                    } else {
                        customViewHolder.likeImageView.setVisibility(View.GONE);
                        customViewHolder.likedUserName.setVisibility(View.GONE);
                        //call unlike url
                        postLikeUnlike(feedItem.getPostID(),unlike);
                    }
                } else {
                    if (customViewHolder.checkBox.isChecked()) {
                        System.out.println("checkbox already checked");
                        customViewHolder.i = customViewHolder.i + 1;
                        customViewHolder.likedUserName.setText(String.valueOf(customViewHolder.i) + " likes");
                        //call like url
                        postLikeUnlike(feedItem.getPostID(),like);
                    } else {
                        System.out.println("i1 value" + customViewHolder.i);
                        customViewHolder.i = customViewHolder.i - 1;
                        customViewHolder.likedUserName.setText(String.valueOf(customViewHolder.i) + " likes");
                        //call unlike url
                        postLikeUnlike(feedItem.getPostID(),like);
                    }
                }
            }
        }
    }
});

I Faced the same issue for RatingBar. Here is my code. You just have to set CHECKBOX instead of RatingBar.

public class MainActivity extends AppCompatActivity {

String ratedValue;
ListView ratingList;

private static final String[] listItems = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12", "13", "14","15", "16", "17", "18", "19", "20" };




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_activity);

    ratingList = (ListView)findViewById(R.id.ratingList);

    ArrayList<RowModel> list = new ArrayList<RowModel>();

    for (String s : listItems) {
        list.add(new RowModel(s));
    }

    ratingList.setAdapter(new RatingAdapter(list));
}

private RowModel getModel (int position) {
    return ((RatingAdapter)ratingList.getAdapter()).getItem(position);
}

class RatingAdapter extends ArrayAdapter<RowModel> {
    RatingAdapter(ArrayList<RowModel> list) {
        super(MainActivity.this, R.layout.list_row, R.id.idView, list);
    }

    class ViewHolder {
        RatingBar ratingBar = null;

        ViewHolder(View view) {
            this.ratingBar = (RatingBar)view.findViewById(R.id.ratingBar1);
            this.ratingBar.setEnabled(false);
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);

        ViewHolder holder = (ViewHolder)row.getTag();

        if (holder == null) {
            holder = new ViewHolder(row);
            row.setTag(holder);

            RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                    Integer myPosition = (Integer)ratingBar.getTag();
                    RowModel model = getModel(myPosition);

                    model.rating = rating;

                    LinearLayout parent = (LinearLayout)ratingBar.getParent();
                    TextView label = (TextView)parent.findViewById(R.id.ratingMessage);

                    label.setText("Rating : " + rating + "/5");
                }
            };

            holder.ratingBar.setOnRatingBarChangeListener(l);
        }
        RowModel  model = getModel(position);

        holder.ratingBar.setTag(new Integer(position));
        holder.ratingBar.setRating(model.rating);

        return (row);
    }
}

class RowModel {
    String label;
    float rating = 0.0f;

    RowModel(String label) {
        this.label = label;
    }

    public String toString() {
        if (rating >= 3.0) {
            return (label.toUpperCase());
        }
        return label;
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM