mercredi 2 septembre 2015

DialogInterface with a ListView

What my problem is
My problem is that I am trying to open up a dialogInterface with a listview that has a row with 1 textview and a CheckBox. The problem I have is that when the CheckBox is clicked, lets say on item 2, item 6 will also be checked, and item 10, and item 14, and all the way when all I want is item 2 to be picked.

What I have tried

 //setup a list adapter and then set that on the list
        ListAdapter listAdapter = new ArrayAdapter(PendingFriendsActivity.this, R.layout.activity_contact_list_item, R.id.contactNameTextView, ContactList);
        AlertDialog.Builder builderSingle = new AlertDialog.Builder(PendingFriendsActivity.this);
        DialogInterface.OnClickListener buttonClicked;
        buttonClicked = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        for(int i = 0; i < sendContactList.size();i++)
                        {
                            String phone, name;
                            name = sendContactList.get(i).get(0);
                            phone = sendContactList.get(i).get(1);
                            sendSMS(phone);
                        }
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        //No button clicked
                        break;
                }
            }
        };
builderSingle.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        RelativeLayout vwParentRow = (RelativeLayout)view.getParent();
        ArrayList<String> contact = new ArrayList<String>();
        final CheckBox contactCheckBox = (CheckBox) vwParentRow.findViewById(R.id.sendCheckBox);
        TextView contactTextView = (TextView) vwParentRow.findViewById(R.id.contactNameTextView);
        Toast.makeText(PendingFriendsActivity.this, contactTextView.getText().toString(),Toast.LENGTH_LONG).show();

        if(contactCheckBox.isChecked())
        {
            contact.add(fullContacts.get(i).get(0));
            contact.add(fullContacts.get(i).get(1));
            sendContactList.add(contact);
        } else {
            sendContactList.remove(i);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
});

        builderSingle.setCancelable(false);
        builderSingle.setTitle("Friend Invite");
        builderSingle.setNegativeButton("Cancel", buttonClicked);
        builderSingle.setPositiveButton("Send Invites", buttonClicked);
        builderSingle.setAdapter(listAdapter, buttonClicked);
        builderSingle.show();




Aucun commentaire:

Enregistrer un commentaire