我创建了一个联系人管理器应用,在此我想使用姓名,电话号码或电子邮件搜索联系人。如果我输入电话号码,则应显示键入号码的联系人,如果是姓名和电子邮件,则显示。
我试过但是使用电子邮件搜索无效。使用姓名和电话进行搜索工作正常。
我的代码是,
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String newText = search.getText().toString();
String regexStr = "^[0-9]*$";
if(newText.trim().matches(regexStr))
adapter.filterUsingNumber(newText);
else {
if(isEmailValid(newText))
adapter.filterUsingEmail(newText);
else
adapter.filter(newText);
}
}
});
}
public static boolean isEmailValid(String email) {
String expression = "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
Matcher matcher = Pattern.compile(expression, Pattern.CASE_INSENSITIVE).matcher(email);
return matcher.matches();
}
错误是,
java.lang.NullPointerException
at contacts.com.contactsmanager.SelectUserAdapter.filterUsingEmail(SelectUserAdapter.java:160)
at contacts.com.contactsmanager.MainActivity$1.afterTextChanged(MainActivity.java:97)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7334)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9087)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:223)
at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
at android.widget.TextView.doKeyDown(TextView.java:5464)
at android.widget.TextView.onKeyDown(TextView.java:5283)
at android.view.KeyEvent.dispatch(KeyEvent.java:2623)
at android.view.View.dispatchKeyEvent(View.java:7343)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1933)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1408)
at android.app.Activity.dispatchKeyEvent(Activity.java:2384)
at android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:49)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:265)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:132)
我的适配器代码是,
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.contacts_list, 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.image);
final SelectUser data = (SelectUser) _data.get(i);
v.title.setText(data.getName());
//v.check.setChecked(data.getCheckedBox());
//v.phone.setText(data.getPhone());
// Set image if exists
try {
if (data.getThumb() != null) {
v.imageView.setImageBitmap(data.getThumb());
Bitmap bm = data.getThumb();
roundedImage = new RoundImage(bm);
v.imageView.setImageDrawable(roundedImage);
} else {
v.imageView.setImageResource(R.drawable.noimage);
}
// Seting round image
//Bitmap bm = BitmapFactory.decodeResource(view.getResources(), R.drawable.noimage); // Load default image
//roundedImage = new RoundImage(bm);
//v.imageView.setImageDrawable(roundedImage);
} catch (OutOfMemoryError e) {
// Add default picture
v.imageView.setImageDrawable(this._c.getDrawable(R.drawable.noimage));
e.printStackTrace();
}
Log.e("Image Thumb", "--------------" + data.getThumb());
/*// Set check box listener android
v.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox checkBox = (CheckBox) view;
if (checkBox.isChecked()) {
data.setCheckedBox(true);
} else {
data.setCheckedBox(false);
}
}
});*/
view.setTag(data);
return view;
}
// Filter Class Using Name
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.getName().toLowerCase(Locale.getDefault())
.contains(charText)) {
_data.add(wp);
}
else {
filterUsingEmail(charText);
Toast.makeText(_c, "adasd", Toast.LENGTH_LONG).show();
}
}
}
notifyDataSetChanged();
}
// Filter Class Using Name
public void filterUsingNumber(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
_data.clear();
if (charText.length() == 0) {
_data.addAll(arraylist);
} else {
for (SelectUser wp : arraylist) {
if (wp.getPhone().toLowerCase(Locale.getDefault())
.contains(charText)) {
_data.add(wp);
}
}
}
notifyDataSetChanged();
}
// Filter Class Using Email
public void filterUsingEmail(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
_data.clear();
if (charText.length() == 0) {
_data.addAll(arraylist);
} else {
for (SelectUser wp : arraylist) {
if (wp.getEmail().toLowerCase(Locale.getDefault())
.contains(charText)) {
_data.add(wp);
}
}
}
notifyDataSetChanged();
}
static class ViewHolder {
ImageView imageView;
TextView title, phone;
CheckBox check;
}
}
有没有办法使用这3个项目进行搜索。提前谢谢。