# HG changeset patch # User Da Risk # Date 1276036135 -7200 # Node ID 3d6983b46a03a390f15e1a23f208bcd5c66be034 # Parent e6a235546a90004941cdd1b1f4a8ed4c1ba6e06c Add filtering capability on contact list. diff -r e6a235546a90 -r 3d6983b46a03 res/layout/chat.xml --- a/res/layout/chat.xml Thu Jun 03 21:59:08 2010 +0200 +++ b/res/layout/chat.xml Wed Jun 09 00:28:55 2010 +0200 @@ -31,6 +31,7 @@ + android:layout_height="fill_parent" android:transcriptMode="disabled" + android:textFilterEnabled="true" /> + diff -r e6a235546a90 -r 3d6983b46a03 src/com/beem/project/beem/ui/ContactList.java --- a/src/com/beem/project/beem/ui/ContactList.java Thu Jun 03 21:59:08 2010 +0200 +++ b/src/com/beem/project/beem/ui/ContactList.java Wed Jun 09 00:28:55 2010 +0200 @@ -77,6 +77,8 @@ import android.view.ViewStub; import android.widget.AdapterView; import android.widget.BaseAdapter; +import android.widget.Filter; +import android.widget.Filterable; import android.widget.Gallery; import android.widget.LinearLayout; import android.widget.ListView; @@ -124,6 +126,7 @@ new ComparatorContactListByStatusAndName(); private final BeemRosterListener mBeemRosterListener = new BeemRosterListener(); private List mListContact; + private String mSelectedGroup; private IRoster mRoster; private Contact mSelectedContact; private IXmppFacade mXmppFacade; @@ -311,6 +314,7 @@ */ private void buildContactList(String group) { mListContact = mContactOnGroup.get(group); + mSelectedGroup = group; Log.d(TAG, "buildContactList for group " + group); mAdapterContactList.notifyDataSetChanged(); } @@ -377,7 +381,7 @@ mContactOnGroup.put(group, tmplist); } List contactByGroups = mContactOnGroup.get(group); - if (contactByGroups == mListContact) { + if (mSelectedGroup.equals(group)) { updateCurrentList(group, contact); continue; } @@ -404,7 +408,7 @@ Contact contact = new Contact(cToDelete); for (Map.Entry> entry : mContactOnGroup.entrySet()) { List contactByGroups = entry.getValue(); - if (contactByGroups == mListContact) { + if (mSelectedGroup.equals(entry.getKey())) { updateCurrentList(entry.getKey(), contact); continue; } @@ -415,7 +419,9 @@ mHandler.post(new Runnable() { public void run() { - mListContact = mContactOnGroup.get(getString(R.string.contact_list_all_contact)); + mSelectedGroup = getString(R.string.contact_list_all_contact); + mListContact = mContactOnGroup.get(mSelectedGroup); + mAdapterContactList.notifyDataSetChanged(); } }); @@ -440,7 +446,7 @@ List groups = contact.getGroups(); for (Map.Entry> entry : mContactOnGroup.entrySet()) { List contactByGroups = entry.getValue(); - if (contactByGroups == mListContact) { + if (mSelectedGroup.equals(entry.getKey())) { updateCurrentList(entry.getKey(), contact); continue; } @@ -486,7 +492,7 @@ List groups = contact.getGroups(); for (Map.Entry> entry : mContactOnGroup.entrySet()) { List contactByGroups = entry.getValue(); - if (contactByGroups == mListContact) { + if (mSelectedGroup.equals(entry.getKey())) { updateCurrentList(entry.getKey(), contact); continue; } @@ -565,12 +571,15 @@ /** * Adapter contact list. */ - private class BeemContactList extends BaseAdapter { + private class BeemContactList extends BaseAdapter implements Filterable { + + private final ContactFilter mFilter; /** * Constructor. */ public BeemContactList() { + mFilter = new ContactFilter(); } /** @@ -594,9 +603,15 @@ */ @Override public long getItemId(int position) { - return position; + return mListContact.get(position).hashCode(); } + @Override + public boolean hasStableIds() { + return true; + } + + /** * {@inheritDoc} */ @@ -618,13 +633,17 @@ return v; } + @Override + public Filter getFilter() { + return mFilter; + } + /** * Adapte curContact to the view. * @param view the row view. * @param curContact the current contact. */ private void bindView(View view, Contact curContact) { - if (curContact != null) { TextView v = (TextView) view.findViewById(R.id.contactlistpseudo); LevelListDrawable mStatusDrawable = (LevelListDrawable) getResources() @@ -636,6 +655,34 @@ v.setText(curContact.getMsgState()); } } + + private class ContactFilter extends Filter { + + @Override + protected Filter.FilterResults performFiltering(CharSequence constraint) { + Log.d(TAG, "performFiltering"); + List result = mListContact; + if (constraint.length() > 0) { + result = new LinkedList(); + for(Contact c : mContactOnGroup.get(mSelectedGroup)) { + if (c.getJID().contains(constraint)) + result.add(c); + } + } + Filter.FilterResults fr = new Filter.FilterResults(); + fr.values = result; + fr.count = result.size(); + return fr; + } + + @Override + protected void publishResults(CharSequence constraint, Filter.FilterResults results) { + Log.d(TAG, "publishResults"); + List contacts = (List) results.values; + mListContact = contacts; + notifyDataSetChanged(); + } + } } /**