get the checked item in the arraylist

余生颓废 提交于 2019-12-25 03:52:42

问题


I am trying to make a app in which i have used a custom list.The list contains text view and checkbox.The list contains few 30-40 datas in it.Now what i want is that i when user checks on a particular checkbox,the associated data with that checkbox should be added to the arrayList.I tried it but when i scroll my list the data from the list disapperas.Why this is happening

COde

public class EmailListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<EmailModel> data;
    DbHandler dbHandler;
    int[] emails;
    ArrayList<String> emailSeperated;

    public EmailListAdapter(Context context, ArrayList<EmailModel> data) {
        this.context = context;
        this.data = data;
        emails = new int[data.size()];
    }

    @Override

    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        final ViewHolder holder;
        dbHandler = new DbHandler(context);
        emailSeperated = new ArrayList<String>();

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.email_custom_list, viewGroup, false);
            holder.tvContact = (TextView) view.findViewById(R.id.tv_email_name);
            holder.checkBox = (CheckBox) view.findViewById(R.id.cb_email_checkbox);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
//        if (emails[i] == 0) {
//            holder.checkBox.setChecked(false);
//        } else {
//            holder.checkBox.setChecked(true);
//        }

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                                       @Override
                                                       public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                                                           if (compoundButton == holder.checkBox) {
                                                               if (b) {
                                                                   emails[i] = 1;
                                                                   //dbHandler.updateContactList(data.get(i).getUserID(), 1);
//                                                                   emailSeperated.add(data.get(i).getEmail());
//                                                                   Log.e("Email values", emailSeperated.toString());
//                                                                   ShareWithinpocketDocs.etEmailLists.setText(emailSeperated.toString());

//

                                                               } else {
                                                                   emails[i] = 0;
//                                                                   emailSeperated.remove(data.get(i).getEmail());
//                                                                   Log.e("Email values", emailSeperated.toString());


                                                               }
                                                           }

                                                       }
                                                   }

        );


        if (emails[i] == 0) {
            holder.checkBox.setChecked(false);
            emailSeperated.remove(data.get(i).getEmail());
            Log.e("Email values", emailSeperated.toString());
            ShareWithinpocketDocs.etEmailLists.setText(emailSeperated.toString());
        } else {
            holder.checkBox.setChecked(true);
            emailSeperated.add(data.get(i).getEmail());
            Log.e("Email values", emailSeperated.toString());
            ShareWithinpocketDocs.etEmailLists.setText(emailSeperated.toString());
        }


        holder.tvContact.setText(data.get(i).getEmail());


        return view;
    }


    private class ViewHolder {
        TextView tvContact;
        CheckBox checkBox;

    }
}

When ever user scrolls the list, the data in the array is deleted.Y such is happening?

来源:https://stackoverflow.com/questions/25488160/get-the-checked-item-in-the-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!