0

I create a list view with checkbox. I faced a problem in my list that when I select any checkbox and when I scroll down my list that checkbox became unselected itself.

Following is my code snippet:

public class HomeListAdapter extends ArrayAdapter<HashMap<String, Object> > {


    boolean[] itemChecked;
    //
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();



    private DisplayImageOptions options;
    private CheckBox check;

    public HomeListAdapter(Context context, ArrayList<HashMap<String, Object>> dataMap) {

            super(context, 0, dataMap);

            itemChecked = new boolean[dataMap.size()];

            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.com_facebook_profile_picture_blank_square)
                    .showImageForEmptyUri(R.drawable.com_facebook_profile_picture_blank_square)
                    .showImageOnFail(R.drawable.com_facebook_profile_picture_blank_square)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .displayer(new RoundedBitmapDisplayer(20)).build();
           this.notifyDataSetChanged();
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            HashMap<String, Object> dataMap = getItem(position);

            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item1, parent, false);
            }


            TextView description = (TextView) convertView.findViewById(R.id.description);
            description.setText(dataMap.get(Home1.TAG_CNAME).toString());
            TextView email = (TextView) convertView.findViewById(R.id.company);
            email.setText(dataMap.get(Home1.TAG_EMAIL).toString());

          check= (CheckBox) convertView.findViewById(R.id.ck);



            check.setTag(getPosition(dataMap));
            check.setChecked(true);



           check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int getPosition = (Integer) buttonView.getTag();


    }
});

            if (dataMap.get(Home1.TAG_LIKE).toString().equals("true")) {
                check.setChecked(true);
            }else{
                check.setChecked(false);
            }


            ImageView logo = (ImageView) convertView.findViewById(R.id.logo);
            ImageLoader.getInstance().displayImage(dataMap.get(Home1.TAG_IMAGE).toString(),logo, options, animateFirstListener);
            return convertView;
        }
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

You need to set the state of your view in your getView() method. You appear to already have an array setup to store the checked items, so you just need to populate it in your onCheckedChanged() method after the line int getPosition = (Integer) buttonView.getTag(); by doing:

itemChecked[getPosition] = isChecked;

Then, in your getView() method, instead of doing:

if (dataMap.get(Home1.TAG_LIKE).toString().equals("true")) {
    check.setChecked(true);
}else{
    check.setChecked(false);
}

you need to use the value in your array to set the checked state:

check.setChecked(itemChecked[position]);

If you need your code to set the initial status of the checked items based on the value of dataMap.get(Home1.TAG_LIKE), it should probably go in the constructor, immediately after you initialize itemChecked and should be something like:

for (int i =0; i < itemChecked.length; ++i) {
    itemChecked[i] = dataMap.get(Home1.TAG_LIKE).toString().equals("true")
}
HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
  • unable to understand u because i am new on android... plz can u help me by example i mean can u do it in my given code. – Tejashree Verma Aug 09 '15 at 22:35
  • 1
    I've updated my answer to make it clear exactly where everything needs to go. Although I could just post your code with the relevant updates, I think it's more useful to anyone who wants to learn to see what needs to be changed and the fact that you need to both store whether the item is checked when the status changes and then set whether the `View` is checked in the `getView()` method. If you still don't understand, please ask a specific question about the bit of my answer you don't understand and I'll try to clarify. – HexAndBugs Aug 10 '15 at 07:35
  • @TejashreeVerma: please do not ask people to do your work for you. If you are stuck and need one-on-one dedicated support, perhaps you need an on-call freelancer, who can help you when you get stuck? You will need to pay them, of course! – halfer Sep 07 '15 at 15:32