在我的应用程序中,我有很多编辑文本视图,用于从电话簿中获取联系人,当我选择第一个edittext视图的联系人时,我不希望第二个联系人看到相同的联系人。怎么做。
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 :(得分: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;
}
}