-1

I am trying to create a application for android. in which at one point i need to open a activity i need to display all the contacts on user's phone in a listview with checkbox, so that multiple contacts can be selected. I have written a code which currently shows the list of all the contacts but without checkbox as you can see in the image attached. Next, when the user selects the required contacts using checkbox and clicks on DONE button the result should be derived in main activity and all the contacts which the user selected should be displayed in the EditText like this Frank <+911234567890>, John <+913456789012>, Ashley <+911237890456>,. How can i achieve what i want? And also the dashes(-) which are currently getting displayed should also disappear.

enter image description here

Tapan Desai
  • 848
  • 2
  • 14
  • 36
  • possible duplicate of [get details of contact selected from list view](http://stackoverflow.com/questions/12338160/get-details-of-contact-selected-from-list-view) – user Sep 12 '12 at 14:45
  • @Luksprog it is not a duplicate in anyways. if you you cant help then you should better stay away. – Tapan Desai Sep 12 '12 at 16:36

3 Answers3

1

Use the following to add checkboxes on all the items:

listView.setChoiceMode(CHOICE_MODE_MULTIPLE);

Not only will this add checkboxes to all the items, but it will handle all the check states for you. You have several methods you can use to get the state of the items:

getCheckedItemCount()
getCheckedItemIds()
getCheckedItemPositions()

And you can use setItemChecked() to set any item's checked state programmatically. Take a look at this tutorial for a guide how to make a multiple selection list.

Samuel
  • 16,923
  • 6
  • 62
  • 75
1

split the string on each '-' using function split("-") and then concatenate it.

HimanshuR
  • 1,390
  • 5
  • 16
  • 35
0

use the code snippet below to retrieve all contacts from the phonebook,append them in a ListView containing checkboxes to enable multiple selection,,it is clear and straight to the point.

public class Display extends Activity implements OnItemClickListener{

List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
MyAdapter ma ;
Button select;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);

    getAllContacts(this.getContentResolver());
    ListView lv= (ListView) findViewById(R.id.lv);
        ma = new MyAdapter();
        lv.setAdapter(ma);
        lv.setOnItemClickListener(this); 
        lv.setItemsCanFocus(false);
        lv.setTextFilterEnabled(true);
        // adding
       select = (Button) findViewById(R.id.button1);
    select.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
                StringBuilder checkedcontacts= new StringBuilder();

            for(int i = 0; i < name1.size(); i++)

                {
                if(ma.mCheckStates.get(i)==true)
                {
                     checkedcontacts.append(name1.get(i).toString());
                     checkedcontacts.append("\n");

                }
                else
                {

                }


            }

            Toast.makeText(Display.this, checkedcontacts,1000).show();
        }       
    });


}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
     ma.toggle(arg2);
}

public  void getAllContacts(ContentResolver cr) {

    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext())
    {
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      name1.add(name);
      phno1.add(phoneNumber);
    }

    phones.close();
 }
class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
{  private SparseBooleanArray mCheckStates;
   LayoutInflater mInflater;
    TextView tv1,tv;
    CheckBox cb;
    MyAdapter()
    {
        mCheckStates = new SparseBooleanArray(name1.size());
        mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return name1.size();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi=convertView;
        if(convertView==null)
         vi = mInflater.inflate(R.layout.row, null); 
         tv= (TextView) vi.findViewById(R.id.textView1);
         tv1= (TextView) vi.findViewById(R.id.textView2);
         cb = (CheckBox) vi.findViewById(R.id.checkBox1);
         tv.setText("Name :"+ name1.get(position));
         tv1.setText("Phone No :"+ phno1.get(position));
         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);
        }

        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        // TODO Auto-generated method stub

         mCheckStates.put((Integer) buttonView.getTag(), isChecked);         
    }   
}   

}

r_allela
  • 792
  • 11
  • 23