我的代码给出了电话联系人详细信息,即电话号码和联系人姓名。我使用了以下代码,但在搜索功能方面无效。请给我更多建议。我收到了来自servers_adp.getFilter()的错误。过滤器(文本);这里显示了getFilter()
的create方法错误package com.syphersafe.securefiles;
public class ContactsActivity extends BaseActivity{
EditText searchcontact_et;
ArrayList<String> namearr,numarr,photoarr;
ArrayAdapter<String> adp;
ServerAdapter servers_adp;
ListView lstContacts;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
searchcontact_et=(EditText)findViewById(R.id.editTextSeachcontact);
servers_adp = new ServerAdapter();
namearr = new ArrayList<String>();
numarr = new ArrayList<String>();
photoarr = new ArrayList<String>();
ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
searchcontact_et.addTextChangedListener(new TextWatcher() {
private boolean mCountIncreased;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void afterTextChanged(Editable s) {
String text = searchcontact_et.getText().toString().toLowerCase(Locale.getDefault());
servers_adp.getFilter().filter(text);
}
});
}
protected void onStart() {
// TODO Auto-generated method stub
Cursor cursornum = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursornum != null) {
while (cursornum.moveToNext()) {
String contactname = cursornum.getString(cursornum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactnum = cursornum.getString(cursornum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
namearr.add(contactname);
numarr.add(contactnum);
//photoarr.add(photoPath);
}
}lstContacts = (ListView) findViewById(R.id.lst_contacts);
lstContacts.setAdapter(servers_adp);super.onStart();
}
public class ServerAdapter extends BaseAdapter{
public int getCount() {
// TODO Auto-generated method stub
return namearr.size();
}
public Object getFilter() {
// TODO Auto-generated method stub
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
//no constraint given, just return all the data. (no search)
results.count = list.size();
results.values = list;
} else {//do the search
List<String> resultsData = new ArrayList<>();
String searchStr = constraint.toString().toUpperCase();
for (String s : list)
if (s.toUpperCase().contains(searchStr)) resultsData.add(s);
results.count = resultsData.size();
results.values = resultsData;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
listFiltered = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
};
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = null;
LayoutInflater inflater = getLayoutInflater();
v = inflater.inflate(R.layout.lv_layout, null);
TextView contact_name = (TextView)v.findViewById(R.id.contact_name);
ImageView contact_photo = (ImageView)v.findViewById(R.id.contact_photo);
TextView contact_num=(TextView)findViewById(R.id.contact_number);
try {
String arlstname[] = namearr.get(position).split(",");
String arlstnumber[] = numarr.get(position).split(",");
for (int i = 0; i < arlstname.length; i++) {
contact_name.setText(arlstname[i]);
contact_num.setText(arlstnumber[i]);
if(arlstname[i].equals("Lav")|| arlstname[i].equals("Teja")){
Drawable myDrawable = getResources().getDrawable(R.drawable.call_person);
contact_photo.setImageDrawable(myDrawable);
}
else{
Drawable myDrawable = getResources().getDrawable(R.drawable.ic_launcher);
contact_photo.setImageDrawable(myDrawable);
}
}
}
catch (Exception e) {
// TODO: handle exception
}
return v;
}
}
}
答案 0 :(得分:0)
首先使您的适配器实现Filterable。添加方法会出错。添加方法
public Filter getFilter() {
}
在该方法中复制粘贴public Object getFilter()方法中的所有代码。完成后,删除公共Object getFilter()方法。
它应该有用。
public class ContactsActivity extends BaseActivity{
EditText searchcontact_et;
ArrayList<String> namearr,numarr,photoarr;
ArrayAdapter<String> adp;
ServerAdapter servers_adp;
ListView lstContacts;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
searchcontact_et=(EditText)findViewById(R.id.editTextSeachcontact);
servers_adp = new ServerAdapter();
namearr = new ArrayList<String>();
numarr = new ArrayList<String>();
photoarr = new ArrayList<String>();
ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
searchcontact_et.addTextChangedListener(new TextWatcher() {
private boolean mCountIncreased;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void afterTextChanged(Editable s) {
String text = searchcontact_et.getText().toString().toLowerCase(Locale.getDefault());
servers_adp.getFilter().filter(text);
}
});
}
protected void onStart() {
// TODO Auto-generated method stub
Cursor cursornum = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursornum != null) {
while (cursornum.moveToNext()) {
String contactname = cursornum.getString(cursornum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactnum = cursornum.getString(cursornum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
namearr.add(contactname);
numarr.add(contactnum);
//photoarr.add(photoPath);
}
}lstContacts = (ListView) findViewById(R.id.lst_contacts);
lstContacts.setAdapter(servers_adp);super.onStart();
}
public class ServerAdapter extends BaseAdapter implements Filterable{
public int getCount() {
// TODO Auto-generated method stub
return namearr.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = null;
LayoutInflater inflater = getLayoutInflater();
v = inflater.inflate(R.layout.lv_layout, null);
TextView contact_name = (TextView)v.findViewById(R.id.contact_name);
ImageView contact_photo = (ImageView)v.findViewById(R.id.contact_photo);
TextView contact_num=(TextView)findViewById(R.id.contact_number);
try {
String arlstname[] = namearr.get(position).split(",");
String arlstnumber[] = numarr.get(position).split(",");
for (int i = 0; i < arlstname.length; i++) {
contact_name.setText(arlstname[i]);
contact_num.setText(arlstnumber[i]);
if(arlstname[i].equals("Lav")|| arlstname[i].equals("Teja")){
Drawable myDrawable = getResources().getDrawable(R.drawable.call_person);
contact_photo.setImageDrawable(myDrawable);
}
else{
Drawable myDrawable = getResources().getDrawable(R.drawable.ic_launcher);
contact_photo.setImageDrawable(myDrawable);
}
}
}
catch (Exception e) {
// TODO: handle exception
}
return v;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
//no constraint given, just return all the data. (no search)
results.count = list.size();
results.values = list;
} else {//do the search
List<String> resultsData = new ArrayList<>();
String searchStr = constraint.toString().toUpperCase();
for (String s : list)
if (s.toUpperCase().contains(searchStr)) resultsData.add(s);
results.count = resultsData.size();
results.values = resultsData;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
listFiltered = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
};
}
}
}