我正在尝试为我的联系人列表实施搜索选项,该搜索选项与联系人图片和联系人姓名一起显示。
我创建了一个文本框来输入搜索文本。
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:扩展活动。
OnCreate中:
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);
}
问题是我在搜索框中输入文字后没有得到结果。我不确定我哪里出错了?
让我知道如何解决这个问题?
谢谢!
答案 0 :(得分:0)
您可能需要查看此链接。 Click Here to see Searchview
这是一个很好的SearchView或使用我的SearchView下面 这是我在listview上的搜索方法
search.addTextChangedListener(new TextWatcher() { //search is a edittext object
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
textlength = search.getText().length(); //textlength is a global variable
filtered_text = new ArrayList<RowItem>(); //global array list
for (int i = 0; i < employee.length(); i++)
{
if(rowItems.get(i).getTitle().toString().toUpperCase().contains(search.getText().toString().toUpperCase())) //rowItem is an object of my holder class for Custom Adapter
{
//Adding image and names which match the search criteria
in filtered_text array list
String mDrawableName;
try {
mDrawableName = employee.getJSONObject(i).getString("image");
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
RowItem item = new RowItem(resID, rowItems.get(i).getTitle());
filtered_text.add(item);
} catch (JSONException e) {e.printStackTrace();}
}
}
adapter = new CustomBaseAdapter(YourClass.this, filtered_text);
listView.setAdapter(adapter);
}
@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
}
});