2014-01-28 46 views
1

在我的應用程序中,我有很多edittext視圖從電話簿獲取聯繫人,當我選擇第一個edittext視圖的聯繫人時,我不希望同一聯繫人在第二個一個。怎麼做。Android刪除已經選擇的聯繫人,同時發送短信

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 
+0

你爲什麼要刪除代碼?不要這樣做,它也可以幫助其他用戶。 –

+0

好吧,讓我再說一遍,實際上我是jst粘貼showSelectedNumber代碼片段。我應該保留整個代碼,還是隻有這個? –

回答

1

將選定的聯繫人儲存在字符串中,例如String selectedContact =「」;

,並檢查該字符串當您選擇接觸

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

試試下面的代碼

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;  
    } 

} 
相關問題