如何仅添加单击的复选框位置

时间:2015-10-30 07:08:00

标签: android listview

我的应用中有联系人。

此处的联系人位于列表视图中,将使用复选框单击选择。

我不知道这里有什么问题。它会在单击“添加”按钮时添加列表视图中的所有联系人。不会选中复选框。

请帮我找到解决方案......

   public class ContactActivity extends Activity implements OnItemClickListener {

        private Gson gson = new Gson();


        private ContactDispAdapter contactDispAdapter;
        private Button btnAdd;
        private Button btnSend;
        private ProgressDialog prgDialog;
        UserDelegate userDelegate = new UserDelegate();
        private Context context;
        private HashMap<String, UserMO> userMosMapAll = new HashMap<>();
        private ArrayList<UserMO> userMOs = new ArrayList<>();
        private UserMO userMO = new UserMO();
        private SharedPreferences sharedpreferences;
        private String eventId;


        @Override
        protected void onStart() {
            super.onStart();
            AppActivityStatus.setActivityStarted();
            AppActivityStatus.setActivityContext(context);
        }

        @Override
        protected void onPause() {
            super.onPause();
            AppActivityStatus.setActivityStoped();

        }

        @Override
        protected void onResume() {
            super.onPause();
            AppActivityStatus.setActivityStarted();
        }

        @Override
        protected void onStop() {
            super.onStop();
            AppActivityStatus.setActivityStoped();
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.contactslistmain);
            context = getApplicationContext();
            sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
                    Context.MODE_PRIVATE);
           // default value of sharedpreference is null if no event id found default value null is returned
            eventId = sharedpreferences.getString("eventId","null");
            Toast.makeText(context, "contact String" + eventId, Toast.LENGTH_LONG).show();
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.remove("eventId");
            editor.commit();
            // in contact page click load button get all contacts from device and
            // send to server and return matching user
            // remove old values from list
            // Instantiate Progress Dialog object
            prgDialog = new ProgressDialog(this);
            // Set Progress Dialog Text
            prgDialog.setMessage("Please wait...");
            // Set Cancelable as False
            prgDialog.setCancelable(false);
            prgDialog.show();
            final String[] mobileNumbers = Utility.getAllContacts(context.getContentResolver());
            new AsyncTask<Void, Void, String>() {
                @Override
                protected String doInBackground(Void... arg0) {
                    return userDelegate.getMatchingExistingUserList(mobileNumbers, context);
                }

                @Override
                protected void onPostExecute(String contactList) {
                    if (contactList != "null") {
                        userMOs = gson.fromJson(contactList, new TypeToken<List<UserMO>>() {
                        }.getType());
                        for (UserMO userMO : userMOs) {
                            userMosMapAll.put(userMO.getMobileNumber(), userMO);
                        }

                        DatabaseHelper dbHelper = new DatabaseHelper(context);
                        prgDialog.dismiss();
                        ListView contactListView = (ListView) findViewById(R.id.contact_List_view);
                        contactDispAdapter = new ContactDispAdapter();
                        contactListView.setAdapter(contactDispAdapter);
                        contactListView.setItemsCanFocus(false);
                        contactListView.setTextFilterEnabled(true);
                        contactListView.setOnItemClickListener(contactListView.getOnItemClickListener());
                    }

                }
            }.execute(null, null, null);


            btnAdd = (Button) findViewById(R.id.btn_Contact_Add);
            btnAdd.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Instantiate Progress Dialog object
                    prgDialog = new ProgressDialog(ContactActivity.this);
                    // Set Progress Dialog Text
                    prgDialog.setMessage("Please wait...");
                    // Set Cancelable as False
                    prgDialog.setCancelable(false);

                    prgDialog.show();
                    DatabaseHelper dbHelper = new DatabaseHelper(context);
                    // by default first primary user is current user in sql lite
                    // user table
                    final UserMO userMO = dbHelper.getRingeeUserData(1);
                    System.out.println(".............." + contactDispAdapter.mCheckStates.size());
                    for (int i = 0; i < userMOs.size(); i++) {
                        if (contactDispAdapter.mCheckStates.get(i) == true) {
                            if (userMosMapAll.containsKey(userMOs.get(i).getMobileNumber())) {
                                userMOs.add(userMosMapAll.get(userMOs.get(i).getMobileNumber()));
                            }
                            System.out.println("Checked......" + userMOs.get(i).getMobileNumber());
                        } else
                        {

                            System.out.println("Not Checked......" + userMOs.get(i).getMobileNumber());
                        }
                    }
                    if (userMOs.size() != 0 && contactDispAdapter.mCheckStates.size()!=0) {
                        new AsyncTask<Void, Void, String>() {
                            @Override
                            protected String doInBackground(Void... arg0) {
                                return userDelegate.addEventUserRelationShipMapping(userMOs, eventId);
                            }

                            @Override
                            protected void onPostExecute(String response) {
                                Toast.makeText(context, "response is " + response.toString(), Toast.LENGTH_LONG).show();
                                prgDialog.dismiss();
                                Intent userDashBoardAct = new Intent(getApplicationContext(), UserDashBoardActivity.class);
                                // Clears History of Activity
                                userDashBoardAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(userDashBoardAct);
                            }

                        }.execute(null, null, null);
                    }else {

                    Toast.makeText(context, "Atleast invite one more person to continue", Toast.LENGTH_LONG).show();
                }
                }
            });
        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            contactDispAdapter.toggle(arg2);
        }

        // add name and mobile number in adapter to show in contact page
        private class ContactDispAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {
            private SparseBooleanArray mCheckStates;
            LayoutInflater mInflater;
            TextView mobileNumTxtV, userNameTxtV;
            CheckBox cb;

            ContactDispAdapter() {
                mCheckStates = new SparseBooleanArray(userMOs.size());
                mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            @Override
            public int getCount() {
                return userMOs.size();
            }

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

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

            // show list values name and mobile number in contact page
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View vi = convertView;
                if (convertView == null)
                    vi = mInflater.inflate(R.layout.contactlist, null,false);
                userNameTxtV = (TextView) vi.findViewById(R.id.text);
                mobileNumTxtV = (TextView) vi.findViewById(R.id.mobile_number);
                cb = (CheckBox) vi.findViewById(R.id.check_box);
                userNameTxtV.setText(userMOs.get(position).getUserName());
                mobileNumTxtV.setText(userMOs.get(position).getMobileNumber());
                cb.setTag(position);
                cb.setChecked(mCheckStates.get(position, false));
                cb.setOnCheckedChangeListener(this);

                return vi;
            }

            public boolean isChecked(int position) {
                return mCheckStates.get(position, false);
            }

            public void setChecked(int position, boolean isChecked) {
                mCheckStates.put(position, isChecked);
                notifyDataSetChanged();
            }

            public void toggle(int position) {
                setChecked(position, !isChecked(position));
            }

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCheckStates.put((Integer) buttonView.getTag(), isChecked);
            }
        }
    }

CheckBox点击(联系人列表)后,我需要点击添加按钮。

在byn_add内部点击监听器我需要点击复选框位置到UserMOs并传递它以添加userDelegate.addEventUserRelationShipMapping(userMOs, eventId);

请帮助我找到....

0 个答案:

没有答案