简体   繁体   中英

CheckBoxes unchecked after scrolling my custom ListView

I created a custom ListView, and put some CheckBoxes in it.
At First, I check all the CheckBoxes and then I scroll down the ListView, then the CheckBoxes become unchecked.

What should I do?

CustomListItem.java

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

    //new

    //private final boolean[] mCheckedState;
   // private final Context mContext;
    boolean[] itemChecked;
    //
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();



    private DisplayImageOptions options;
        public HomeListAdapter(Context context, ArrayList<HashMap<String, Object>> dataMap) {

            super(context, 0, dataMap);
//new
            itemChecked = new boolean[dataMap.size()];
          //  mContext = context;
     //
            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.log)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .displayer(new RoundedBitmapDisplayer(20)).build();
           this.notifyDataSetChanged();
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            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());

         final CheckBox check= (CheckBox) convertView.findViewById(R.id.ck);
            //new
            /*
check.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (check.isChecked())
            itemChecked[position] = true;
        Toast.makeText(getContext(),itemChecked,);
        else
            itemChecked[position] = false;

    }
});*/
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (check.isChecked())
            itemChecked[position] = true;
       // Toast.makeText(getContext(),itemChecked,);
        else
        itemChecked[position] = false;

    }

});


            //
            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;
        }

This is my Adapter Classs please help me guys to resolve this issue....

    @Override
    public void onCreate(Bundle savedInstanceState) {

        //Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
        session = new SessionManager(getApplicationContext());

        // get user data from session
        HashMap<String, String> user = session.getUserDetails();


        // apikey
        apikey = user.get(SessionManager.KEY_api);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        ImageView back = (ImageView) findViewById(R.id.sel);
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
//                Intent intent = new Intent(Home1.this, Selected.class);
//                finish();
//                startActivity(intent);
                try {
                    ListView lv = getListView();
                    ids = "";
                    int count = 0;
                    String text = "";
                    //CheckBox cbView;

                    TextView textView;
                    int total = lv.getCount();
                    for (int i = 0; i < total; i++) {
                        if (lv.getChildAt(i) != null) {
                            textView = (TextView) lv.getChildAt(i).findViewById(R.id.company);
                            text = textView.getText().toString();

                            cbView = (CheckBox) lv.getChildAt(i).findViewById(R.id.ck);




                            if (cbView.isChecked()) {
                                count += 1;
                                ids += text + ", ";
                            }
                        }
                    }
                    ids = ids.substring(0, ids.length()-2);
                    //Toast.makeText(getApplicationContext(), "Company emails: " + ids + " Total Checked: " + count, Toast.LENGTH_SHORT).show();
                    new DownloadOperation().execute();
                }
                catch(Exception ex){
                    System.out.println("My Message");
                    ex.printStackTrace();
                    System.out.println(ex.getMessage());
                }
            }

        });

You can overcome this easily by using the ChoiceMode option CHOICE_MODE_MULTIPLE for the ListView as used in the API Demo -

public class List11 extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, GENRES));

        final ListView listView = getListView();
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
    private static final String[] GENRES = new String[] {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };
}

EDIT Alternate for your context- Have an extra boolean object with your HashMap<String, Object> isChecked and initialize it to false when you populate your list .

Set your CheckBox in getView() method as-

check.setChecked(Boolean.parseBoolean(dataMap.get("isChecked"));

Now in the listener OnCheckedChangeListener() set the value of your dataMap true or false accordingly.

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