我想在ListView
中显示联系电话。它工作得很完美。但是当它从设备读取联系时我想要条件。条件是
如果号码digit <10
{ - 不要添加到列表中 - }。
否则if(数字从0开始,然后用91替换){ - 添加到列表}。
如果{号码从+开始,则将其删除} { - 添加到列表}。
其他{其他直接添加到列表}。
这是我的代码:
public class ReferFriend extends AppCompatActivity {
ArrayList<SelectUser> selectUsers;
List<SelectUser> temp;
ListView listView;
Cursor phones, email;
ContentResolver resolver;
SearchView search;
SelectUserAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.refer_friend);
selectUsers = new ArrayList<SelectUser>();
resolver = this.getContentResolver();
listView = (ListView) findViewById(R.id.contacts_list);
phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
LoadContact loadContact = new LoadContact();
loadContact.execute();
}
// Load data on background
class LoadContact extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... voids) {
// Get Contact list from Phone
if (phones != null) {
Log.e("count", "" + phones.getCount());
if (phones.getCount() == 0) {
Toast.makeText(ReferFriend.this, "No contacts in your contact list.", Toast.LENGTH_LONG).show();
}
while (phones.moveToNext()) {
Bitmap bit_thumb = null;
String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String image_thumb = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));
try {
if (image_thumb != null) {
bit_thumb = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb));
} else {
Log.e("No Image Thumb", "--------------");
}
} catch (IOException e) {
e.printStackTrace();
}
SelectUser selectUser = new SelectUser();
selectUser.setThumb(bit_thumb);
selectUser.setContactName(name);
selectUser.setPhone(phoneNumber);
selectUser.setContactEmail(id);
selectUser.setCheckedBox(false);
selectUsers.add(selectUser);
}
} else {
Log.e("Cursor close 1", "----------------");
}
//phones.close();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter = new SelectUserAdapter(selectUsers, ReferFriend.this);
listView.setAdapter(adapter);
// Select item on listclick
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("search", "here---------------- listener");
SelectUser data = selectUsers.get(i);
}
});
listView.setFastScrollEnabled(true);
}
}
@Override
protected void onStop() {
super.onStop();
phones.close();
}
public class SelectUserAdapter extends BaseAdapter {
public List<SelectUser> _data;
private ArrayList<SelectUser> arraylist;
Context _c;
ViewHolder v;
RoundImage roundedImage;
public SelectUserAdapter(List<SelectUser> selectUsers, Context context) {
_data = selectUsers;
_c = context;
this.arraylist = new ArrayList<SelectUser>();
this.arraylist.addAll(_data);
}
@Override
public int getCount() {
return _data.size();
}
@Override
public Object getItem(int i) {
return _data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (view == null) {
LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.contact_info, null);
Log.e("Inside", "here--------------------------- In view1");
} else {
view = convertView;
Log.e("Inside", "here--------------------------- In view2");
}
v = new ViewHolder();
v.title = (TextView) view.findViewById(R.id.name);
v.check = (CheckBox) view.findViewById(R.id.check);
v.phone = (TextView) view.findViewById(R.id.no);
v.imageView = (ImageView) view.findViewById(R.id.pic);
final SelectUser data = (SelectUser) _data.get(i);
v.title.setText(data.getContactName());
v.check.setChecked(data.getCheckedBox());
//v.phone.setText(data.getPhone());
//I want to apply that condition here
/*if (data.getPhone().length() <= 10) {
v.phone.setText("Contact not Added to list");
} else if(data.getPhone().length() == 10){
v.phone.setText(data.getPhone());
} else if(data.getPhone().equalsIgnoreCase("+")){
v.phone.setText(data.getPhone());
}else if(data.getPhone().startsWith("0")){
v.phone.setText(data.getPhone().replace(0,91));
}*/
view.setTag(data);
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
_data.clear();
if (charText.length() == 0) {
_data.addAll(arraylist);
} else {
for (SelectUser wp : arraylist) {
if (wp.getContactName().toLowerCase(Locale.getDefault())
.contains(charText)) {
_data.add(wp);
}
}
}
notifyDataSetChanged();
}
class ViewHolder {
ImageView imageView;
TextView title, phone;
CheckBox check;
}
}
}
答案 0 :(得分:0)
在添加联系人写下确认功能之前,它将验证您所需的所有条件。如果验证成功,则该函数将返回true,否则返回false。如果你返回true,那么将该联系人添加到selectUser对象,否则不要添加。
答案 1 :(得分:0)
SelectUser selectUser = new SelectUser();
selectUser.setThumb(bit_thumb);
selectUser.setContactName(name);
selectUser.setPhone(phoneNumber);
selectUser.setContactEmail(id);
selectUser.setCheckedBox(false);
//object preparation
Boolean flag = prepareObject(selectUser);
if(flag){
//add valid object to arraylist
selectUsers.add(selectUser);
}else{
//invalid contact details
}
//method to validate and update object
public Boolean prepareObject(SelectUser obj){
if(obj.getPhone.lenght < 10) {
//dont add to list
}else{
String temp = obj.getPhone.charAt(0);
if(temp == "0"){
//replace it with 91 and update object
return true
}else if(temp == "+"){
//remove it and update object
return true
}else{
//add to object
return true
}
}
return false;
}
我写下了你的问题的逻辑代码。只是对它进行必要的修改,它肯定会有用。
您在不检查条件的情况下直接将对象添加到arraylist。 你在适配器上写条件是错误的,apdater只会从arraylist中获取数据。如果你有有效的数据到arraylist那么就没有必要在适配器上放置条件了。