我正在尝试为与联系人图像和联系人姓名一起显示的联系人列表实现搜索选项。
我创建了一个文本框来输入搜索文本。
contactnameadpater.java:扩展基本适配器。
其中有以下代码:
public class ContactNamesAdapter extends BaseAdapter implements Filterable
{
private Activity activity;
private ArrayList<HashMap<String, String>> originalData;
private ArrayList<HashMap<String, String>> filteredData;
private static LayoutInflater inflater=null;
public ContactNamesAdapter(Activity a, ArrayList<HashMap<String, String>> d)
{
activity = a;
originalData=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return originalData.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.contacts_row, null);
ImageView profile = (ImageView)vi.findViewById(R.id.ContactImage);
Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(originalData.get(position).get("id")));
InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(ContactNamesAdapter.inflater.getContext().getContentResolver(),my_contact_Uri);
if(photo_stream != null)
{
BufferedInputStream buf =new BufferedInputStream(photo_stream);
Bitmap my_btmp = BitmapFactory.decodeStream(buf);
profile.setImageBitmap(my_btmp);
}
else
{
profile.setImageResource(R.drawable.no_pic);
}
TextView name = (TextView)vi.findViewById(R.id.name);
name.setText(originalData.get(position).get("name"));
return vi;
}
@Override
public Filter getFilter()
{
return new Filter()
{
private ArrayList<HashMap<String, String>> filteredResultsData;
@Override
protected FilterResults performFiltering(CharSequence charSequence)
{
FilterResults results = new FilterResults();
//If there's nothing to filter on, return the original data for your list
if(charSequence == null || charSequence.length() == 0)
{
results.values = originalData;
results.count = originalData.size();
}
else
{
ArrayList<HashMap<String,String>> filterResultsData = new ArrayList<HashMap<String,String>>();
for(HashMap<String,String> data : originalData)
{
if(originalData == filterResultsData )
{
filterResultsData.add(data);
}
}
results.values = filterResultsData;
results.count = filteredResultsData.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults)
{
filteredData = (ArrayList<HashMap<String,String>>)filterResults.values;
notifyDataSetChanged();
}
};
}
contactname.java:扩展一个活动。
创建时:
DetailsList = new ArrayList<HashMap<String, String>>();
contactList = (ListView) findViewById(R.id.ContactNamelist);
profileImage = (ImageView) findViewById(R.id.ContactImage);
inputSearch = (EditText) findViewById(R.id.inputSearch);
getContactName();
contactList.setTextFilterEnabled(true);
inputSearch.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter.getFilter().filter(cs.toString());
//adapter.getFilter().filter(cs);
Log.e("getfilter","getfilter");
contactList.setAdapter(adapter);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
}
在getContactName()下:
public void getContactName()
{
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
final String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID
};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = getContentResolver().query(uri, projection, selection, null, sortOrder);
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String Sid = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("id", Sid);
DetailsList.add( map);
}
}
cur.close();
adapter = new ContactNamesAdapter(this, DetailsList);
// updating listview
contactList.setAdapter(adapter);
}
问题是在搜索框中输入文本后我没有得到结果。我不确定我哪里出错了?
让我知道如何解决这个问题?
谢谢!