Question

In my application i have many edittext views for fetching contacts from phone-book and when i choose contact for first edittext view , i don't want that same contact to be visible for the second one . How to do it.

    String selectedNum = " ";
    public void showSelectedNumber(String name, String number, int type) {
        if (layoutLinear == null) {
            Log.i("layoutLinear is null", "null");
        } else {
            Log.i("layoutLinear is not  null", "not null");

        }

        EditText userNumber = (EditText) layoutLinear.getChildAt(0);
        if (userNumber == null) {
            Log.i("edittext is null", "null");
        } else {
            Log.i("edittext is not  null", "not null");

        }
        String typeNumber = (String) ContactsContract.CommonDataKinds.Phone
                .getTypeLabel(getResources(), type, "");
//preventing number duplicacy and raising toast 
        if(selectedNum.contains(number)){
//          do nothing
//          alert user that number is already selected
            Toast.makeText(getApplicationContext(), "Selected Contact Already Exists", Toast.LENGTH_SHORT).show();
        }else 
            userNumber.setText(name+":"+number+" "+typeNumber);
        selectedNum= selectedNum+number;




    }



}// final parentheses
Was it helpful?

Solution

store selected contacts in a string like String selectedContact="";

and check this string when you select contact

if(selectedContact.contains(newContact))
{ 
  //do nothing
}
else{ 
  //set to edittext 
  and selectedContact+newContact ;
}

try following code

String selectedNum="";//class or global variable

public void showSelectedNumber(String name, String number, int type) {
    if (layoutLinear == null) {
        Log.i("layoutLinear is null", "null");
    } else {
        Log.i("layoutLinear is not  null", "not null");

    }

    EditText userNumber = (EditText) layoutLinear.getChildAt(0);
    if (userNumber == null) {
        Log.i("edittext is null", "null");
    } else {
        Log.i("edittext is not  null", "not null");

    }
    String typeNumber = (String) ContactsContract.CommonDataKinds.Phone
            .getTypeLabel(getResources(), type, "");
    if(selectedNum.contains(number)){
      // nothing to do 
      // alert user number already selected
     }else{
       userNumber.setText(name + ": " + number + " " + typeNumber);
       selectedNum=selectedNum+number;    
     }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top