我打算将搜索添加到我正在处理的联系人列表中。我添加了searchview textchanged监听器,我该如何解决这个问题,以便根据listview中每个项目的字母进行搜索?因为我错过了什么?
enum LvType {
HEADER_GROUP,
HEADER_UNGROUPED,
HEADER_twoway_CHAT,
HEADER_MANAGED_CHATROOM,
ITEM_CHATROOM,
ITEM_twoway_CHAT,
ITEM_USER,
}
class ListItemsHolder {
private ArrayList<String> lvJids;
private ArrayList<String> lvNames;
private ArrayList<Presence> lvPresences;
private ArrayList<LvType> lvTypes;
public ListItemsHolder() {
}
public void init() {
lvTypes = new ArrayList<LvType>();
lvJids = new ArrayList<String>();
lvNames = new ArrayList<String>();
lvPresences = new ArrayList<Presence>();
}
public int setInfo(String jid, String name, LvType type, Presence presence) {
lvJids.add(jid);
lvNames.add(name);
lvTypes.add(type);
lvPresences.add(presence);
return lvJids.size();
}
public String getJid (int pos) { return ((lvJids == null) ? null : lvJids.get(pos) ); }
public String getName (int pos) { return ((lvNames == null) ? null : lvNames.get(pos) ); }
public LvType getType (int pos) { return ((lvTypes == null) ? null : lvTypes.get(pos) ); }
public Presence getPresence (int pos) { return ((lvPresences == null) ? null : lvPresences.get(pos)); }
public int getSize() { return (lvJids == null) ? 0 : lvJids.size(); }
}
public class ContactFragment extends RMBaseFragment<MethodsForContactFragment> implements OnItemClickListener, OnItemLongClickListener, ContactInterface,FragmentDrawer.FragmentDrawerListener
, OnClickListener, OnMenuItemClickedListener, Dialog.OnDismissListener,SearchView.OnQueryTextListener,
MainActivity.BackPressListener<android.support.v4.app.Fragment>
{
public final static String TAG_Contact_FRAGMENT = "TAG_Contact_FRAGMENT";
private static final String MY_PRESENCE_DIALOG_FRAGMENT = "MyPresenceDialogFragment";
private RecyclerView m_recycler;
public static ContactFragment newInstance(final FragmentManager manager) {
final ContactFragment fragment = new ContactFragment();
final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD);
fragmentInfo.setFragmentTag(TAG_Contact_FRAGMENT);
fragmentInfo.setActionBarTitle("contact list");
fragmentInfo.setAnimation(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
fragmentInfo.setPopAnimation(R.anim.slide_in_from_left, R.anim.no_animation2);
FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
class ViewHolder {
public TextView text;
public ImageView image;
}
private int numOfConferences = 0;
private int numOfLegacyChats = 0;
private int numOfGroups = 0;
private int numOfUnfiled = 0;
private Vector<BookmarkedConference> mConferences = null;
private Vector<BookmarkedConference> mLegacyChats = null;
private String[] ContactGroupsNames = null;
private TreeMap<String, ContactGroup> treeMapOfContactGroups = null;
//return new TreeMap<String, ContactGroup>(groups).keySet().toArray(new String[0]);
private DrawerLayout mDrawerLayout;
EditText inputSearch;
private int numOfBookmarks = 0;
private Vector mBoomarks = null;
private ListItemsHolder listItemsHolder = new ListItemsHolder();
private static final String twowayChatsHeader = "twoway Chats";
private static final String managedChatroomsHeader = "Managed Chatrooms";
private static final String ungroupedContactsHeader= "Ungrouped Contacts";
private MenuHelper mMenuHelper;
private final ArrayList<String> mMenuItems = new ArrayList<String>();
private ImageView mContextMenuButton;
private ImageView mMyPresenceButton;
private FontTextView mMyPresenceTextView;
private ImageView mContactAccountButton;
private static class MenuItems{
final static String PRESENCE = Application.getAppResources().getString(R.string.rm_news_menu_presence);
final static String LOGOUT = Application.getAppResources().getString(R.string.rm_news_menu_logout);
static final int PRESENCE_POSITION = 0;
static final int LOGOUT_POSITION = 1;
}
private void createMenu() {
mMenuItems.add(MenuItems.PRESENCE);
mMenuItems.add(MenuItems.LOGOUT);
mMenuHelper = new MenuHelper(getActivity(),mMenuItems,this,this);
}
public class MySimpleArrayAdapter extends BaseAdapter {
private final Activity context;
private ItemFilter mFilter;
public MySimpleArrayAdapter(Activity context) {
this.context = context;
prepareDataForListView();
}
private int total;
private void sortDataDefault(wrapper dw) {
if (dw == null)
return;
Contact Contact = dw.getContact();
Hashtable ht;
Vector<ContactEntry> entries;
if (Contact != null && ((ht = Contact.getGroupsEx()) != null) ) {
numOfGroups = Contact.getGroupCount();
treeMapOfContactGroups = new TreeMap<String, ContactGroup>(ht);
ContactGroupsNames = treeMapOfContactGroups.keySet().toArray(new String[0]);
for (int ii = 0; ii < numOfGroups; ii++) {
listItemsHolder.setInfo(ContactGroupsNames[ii], ContactGroupsNames[ii], LvType.HEADER_GROUP, null);
ContactGroup group = Contact.getGroup(ContactGroupsNames[ii]);
entries = group.getEntriesEx();
Collections.sort(entries, comparatorContactEntryAlpha);
Iterator<ContactEntry> it = entries.iterator();
ContactEntry tmpEntry;
while(it.hasNext()) {
tmpEntry = it.next();
listItemsHolder.setInfo(tmpEntry.getUser(), tmpEntry.getName(), LvType.ITEM_USER, Contact.getPresence(tmpEntry.getUser()));
}
}
}
entries = Contact.getUnfiledEntriesEx();
if (entries != null) {
listItemsHolder.setInfo(ungroupedContactsHeader, ungroupedContactsHeader, LvType.HEADER_GROUP, null);
Collections.sort(entries, comparatorContactEntryAlpha);
Iterator<ContactEntry> it = entries.iterator();
ContactEntry tmpEntry;
while(it.hasNext()) {
tmpEntry = it.next();
listItemsHolder.setInfo(tmpEntry.getUser(), tmpEntry.getName(), LvType.ITEM_USER, Contact.getPresence(tmpEntry.getUser()));
}
}
mConferences = dw.getConferences();
if (mConferences != null && mConferences.size() > 0) {
listItemsHolder.setInfo(twowayChatsHeader, twowayChatsHeader, LvType.HEADER_twoway_CHAT, null);
Collections.sort(mConferences, comparatorBookMarksAlpha);
Iterator<BookmarkedConference> it = mConferences.iterator();
BookmarkedConference bk;
while(it.hasNext()) {
bk = it.next();
listItemsHolder.setInfo(bk.getRoomId(), bk.getName(), LvType.ITEM_twoway_CHAT, null);
}
}
mLegacyChats = dw.getLegacyChats(); // mUNitroChatManager.getConferenceBookmarksAsVector(Bookmarks.Type.LEGACY_ROOM);
if (mLegacyChats != null && mLegacyChats.size() > 0) {
listItemsHolder.setInfo(managedChatroomsHeader, managedChatroomsHeader, LvType.HEADER_MANAGED_CHATROOM, null);
Collections.sort(mLegacyChats, comparatorBookMarksAlpha);
Iterator<BookmarkedConference> it = mLegacyChats.iterator();
BookmarkedConference bk;
while(it.hasNext()) {
bk = it.next();
listItemsHolder.setInfo(bk.getRoomId(), bk.getName(), LvType.ITEM_CHATROOM, null);
}
}
}
private void prepareDataForListView() {
numOfConferences = numOfLegacyChats = 0;
mConferences = null;
mLegacyChats = null;
listItemsHolder.init();
try {
sortDataDefault(controller.getContactModel());
}
catch (Exception ex) {
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.my_poc_rowlayout, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.label);
viewHolder.image = (ImageView) rowView.findViewById(R.id.icon);
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
String jid = listItemsHolder.getJid(position);
String name = listItemsHolder.getName(position);
holder.image.setVisibility(View.VISIBLE);
switch (listItemsHolder.getType(position)) {
case ITEM_USER:
Status status = MessengerUtils.presence2Status(listItemsHolder.getPresence(position));
if (status == Status.AVAILABLE)
holder.image.setImageResource(R.drawable.online);
else if (status == Status.BUSY)
holder.image.setImageResource(R.drawable.busy);
else if (status == Status.AWAY)
holder.image.setImageResource(R.drawable.away);
else
holder.image.setImageResource(R.drawable.offline);
break;
case ITEM_twoway_CHAT:
holder.image.setImageResource(R.drawable.twoway_chat);
break;
case ITEM_CHATROOM:
holder.image.setImageResource(R.drawable.chatroom);
break;
case HEADER_GROUP: // fall thru
case HEADER_twoway_CHAT: // keep falling
case HEADER_MANAGED_CHATROOM:
holder.image.setVisibility(View.GONE);
break;
default:
//TODO Throw some nasty exception
break;
}
if (name != null && name.length() > 1){
holder.text.setText(name);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) holder.text.getLayoutParams();
params.leftMargin = 20;
}else
holder.text.setText(jid);
return rowView;
}
public String firstUpper(String text) {
return (text.substring(0,1).toUpperCase() + text.substring(1).toLowerCase());
}
@Override
public int getCount() {
if (listItemsHolder == null)
return 0;
return listItemsHolder.getSize();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return null; // don't really need it
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Set up search view
inflater.inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.search_messenger);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
@Override
public void onItemClick(final AdapterView<?> parent, final View arg1, final int position, final long id) {
if (listItemsHolder == null) // paranoia
return;
String jid = listItemsHolder.getJid(position);
String name = listItemsHolder.getName(position);
LvType type = listItemsHolder.getType(position);
if (type == LvType.ITEM_USER) {
controller.openChat(jid, name);
mDrawerLayout.closeDrawer(Gravity.RIGHT);
}
}
private ListView listView_Contact;
private TextView empty_tv;
private MySimpleArrayAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.poc_Contact_fragment, container, false);
mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout_messenger);
m_recycler = (RecyclerView)view.findViewById(R.id.recyclerConversations);
m_recycler.setHasFixedSize(true);
m_recycler.setLayoutManager(new LinearLayoutManager(this.getActivity()));
m_recycler.setAdapter(m_recyclerAdapter);
listView_Contact = (ListView)view.findViewById(R.id.listView_Contact);
empty_tv = (TextView)view.findViewById(R.id.empty_tv);
inputSearch = (EditText) view.findViewById(R.id.search_text_messenger);
mAdapter = new MySimpleArrayAdapter(getActivity());
listView_Contact.setAdapter(mAdapter);
listView_Contact.setTextFilterEnabled(true);
listView_Contact.setOnItemClickListener(this);
listView_Contact.setOnItemLongClickListener(this);
//headerView = (HeaderView) view.findViewById(R.id.news_categories_container);
mContextMenuButton = (ImageView) view.findViewById(R.id.context_menu_button);
mContextMenuButton.setOnClickListener(this);
mMyPresenceButton = (ImageView) view.findViewById(R.id.my_presence_button);
mMyPresenceButton.setOnClickListener(this);
mMyPresenceTextView = (FontTextView) view.findViewById(R.id.my_presence_textview);
mMyPresenceTextView.setOnClickListener(this);
setMyPresenceStatus();
mContactAccountButton = (ImageView) view.findViewById(R.id.Contact_contact_button);
mContactAccountButton.setOnClickListener(this);
createMenu();
return view;
}
public void chatMessageReceived(BaseNitroChat chat) {
String from = chat.getName();
String says = chat.getLastMessage().getBody();
Toast.makeText(getActivity(), "" + from + " says " + says, Toast.LENGTH_SHORT).show();
}
@Override
public void ContactModelUpdated() {
if (mAdapter == null)
return;
mAdapter.prepareDataForListView();
mAdapter.notifyDataSetChanged();
m_recyclerAdapter.notifyDataSetChanged();
}
}
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
mAdapter.getFilter().filter(s);
return false;
}
}