Search code examples
javaandroidlistviewarraylist

How to load selected Phone contacts into list view


I am able to retrieve the phone contacts with the help of the given code. Whenever I click a contact it shows in the list view one by one While I don't want that. My question is how can we select multiple contacts at a time and get them in our list view at once?

    public class MainActivity extends AppCompatActivity {

private static final int RESULT_PICK_CONTACT =1;
private TextView phone;
private Button select;
String shareFact;
private ListView listmain;
ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_main);

    phone = findViewById (R.id.phone);
    select = findViewById (R.id.select);
    listmain = findViewById (R.id.list);
    shareFact = phone.getText().toString();



    select.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View v) {
            Intent in = new Intent (Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult (in, RESULT_PICK_CONTACT);
        }
    });

}

private void setListAdapter(ArrayAdapter<String> adapter) {
}

@Override
protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
    if(resultCode==RESULT_OK)
    {
        switch (requestCode) {
            case RESULT_PICK_CONTACT:
                contactPicked (data);
                break;
        }
    }
    else
    {
        Toast.makeText (this, "Failed To pick contact", Toast.LENGTH_SHORT).show ();
    }
}

private void contactPicked(Intent data) {
    Cursor cursor = null;

    try {
        String phoneNo = null;
        Uri uri = data.getData ();
        cursor = getContentResolver ().query (uri, null, null,null,null);
        cursor.moveToFirst ();
        int phoneIndex = cursor.getColumnIndex (ContactsContract.CommonDataKinds.Phone.NUMBER);

        phoneNo = cursor.getString (phoneIndex);

        phone.setText (phoneNo);
        list = new ArrayList<String>();
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                list);
        listmain.setAdapter(arrayAdapter);
        list.add(phoneNo);


    } catch (Exception e) {
        e.printStackTrace ();
    }
}

How to get selected contacts in list view at once while selecting them in mobile? Please give some guidance. Thanks


Solution

  • I have solved this by changing in code as shown.....

           private void contactPicked(Intent data) {
        Cursor cursor = null;
    
        try {
            String phoneNo = null;
            Uri uri = data.getData ();
            cursor = getContentResolver ().query (uri, null, null,null,null);
            cursor.moveToFirst ();
            int phoneIndex = cursor.getColumnIndex (ContactsContract.CommonDataKinds.Phone.NUMBER);
    
            phoneNo = cursor.getString (phoneIndex);
    
            phone.setText (phoneNo);
    
    
            list.add(phoneNo);
            listmain.setAdapter(arrayAdapter);
            arrayAdapter.notifyDataSetChanged();
    
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }