读书人

listView自定义过滤状况扩展arrayAdap

发布时间: 2012-09-12 09:21:30 作者: rapoo

listView自定义过滤情况扩展arrayAdapter

+ o.getLatestChapter());?
?
? ? ? ? ? ? if(enableSections && getSectionForPosition(position) != getSectionForPosition(position + 1))?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? TextView h = (TextView) v.findViewById(R.id.MangaRow_Header);?
? ? ? ? ? ? ? ? h.setText(sections[getSectionForPosition(position)]);?
? ? ? ? ? ? ? ? h.setVisibility(View.VISIBLE);?
? ? ? ? ? ? }?
? ? ? ? ? ? else?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? TextView h = (TextView) v.findViewById(R.id.MangaRow_Header);?
? ? ? ? ? ? ? ? h.setVisibility(View.GONE);?
? ? ? ? ? ? }?
? ? ? ? }?
?
? ? ? ? return v;?
? ? }?
?
? ? @Override?
? ? public void notifyDataSetInvalidated()?
? ? {?
? ? ? ? if(enableSections)?
? ? ? ? {?
? ? ? ? ? ? for (int i = items.size() - 1; i >= 0; i--)?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? Manga element = items.get(i);?
? ? ? ? ? ? ? ? String firstChar = element.getName().substring(0, 1).toUpperCase();?
? ? ? ? ? ? ? ? if(firstChar.charAt(0) > 'Z' || firstChar.charAt(0) < 'A')?
? ? ? ? ? ? ? ? ? ? firstChar = "@";?
? ? ? ? ? ? ? ? alphaIndexer.put(firstChar, i);?
? ? ? ? ? ? }?
?
? ? ? ? ? ? Set<String> keys = alphaIndexer.keySet();?
? ? ? ? ? ? Iterator<String> it = keys.iterator();?
? ? ? ? ? ? ArrayList<String> keyList = new ArrayList<String>();?
? ? ? ? ? ? while (it.hasNext())?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? keyList.add(it.next());?
? ? ? ? ? ? }?
?
? ? ? ? ? ? Collections.sort(keyList);?
? ? ? ? ? ? sections = new String[keyList.size()];?
? ? ? ? ? ? keyList.toArray(sections);?
?
? ? ? ? ? ? super.notifyDataSetInvalidated();?
? ? ? ? }?
? ? }?
?
? ? public int getPositionForSection(int section)?
? ? {?
? ? ? ? if(!enableSections) return 0;?
? ? ? ? String letter = sections[section];?
?
? ? ? ? return alphaIndexer.get(letter);?
? ? }?
?
? ? public int getSectionForPosition(int position)?
? ? {?
? ? ? ? if(!enableSections) return 0;?
? ? ? ? int prevIndex = 0;?
? ? ? ? for(int i = 0; i < sections.length; i++)?
? ? ? ? {?
? ? ? ? ? ? if(getPositionForSection(i) > position && prevIndex <= position)?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? prevIndex = i;?
? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? }?
? ? ? ? ? ? prevIndex = i;?
? ? ? ? }?
? ? ? ? return prevIndex;?
? ? }?
?
? ? public Object[] getSections()?
? ? {?
? ? ? ? return sections;?
? ? }?
?
? ? @Override?
? ? public Filter getFilter()?
? ? {?
? ? ? ? if(filter == null)?
? ? ? ? ? ? filter = new MangaNameFilter();?
? ? ? ? return filter;?
? ? }?
?
? ? private class MangaNameFilter extends Filter?
? ? {?
?
? ? ? ? @Override?
? ? ? ? protected FilterResults performFiltering(CharSequence constraint) {?
? ? ? ? ? ? // NOTE: this function is *always* called from a background thread, and?
? ? ? ? ? ? // not the UI thread.?
? ? ? ? ? ? constraint = constraint.toString().toLowerCase();?
? ? ? ? ? ? FilterResults result = new FilterResults();?
? ? ? ? ? ? if(constraint != null && constraint.toString().length() > 0)?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ArrayList<Manga> filt = new ArrayList<Manga>();?
? ? ? ? ? ? ? ? ArrayList<Manga> lItems = new ArrayList<Manga>();?
? ? ? ? ? ? ? ? synchronized (this)?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? lItems.addAll(items);?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? for(int i = 0, l = lItems.size(); i < l; i++)?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? Manga m = lItems.get(i);?
? ? ? ? ? ? ? ? ? ? if(m.getName().toLowerCase().contains(constraint))?
? ? ? ? ? ? ? ? ? ? ? ? filt.add(m);?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? result.count = filt.size();?
? ? ? ? ? ? ? ? result.values = filt;?
? ? ? ? ? ? }?
? ? ? ? ? ? else?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? synchronized(this)?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? result.values = items;?
? ? ? ? ? ? ? ? ? ? result.count = items.size();?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? }?
? ? ? ? ? ? return result;?
? ? ? ? }?
?
? ? ? ? @SuppressWarnings("unchecked")?
? ? ? ? @Override?
? ? ? ? protected void publishResults(CharSequence constraint, FilterResults results) {?
? ? ? ? ? ? // NOTE: this function is *always* called from the UI thread.?
? ? ? ? ? ? filtered = (ArrayList<Manga>)results.values;?
? ? ? ? ? ? notifyDataSetChanged();?
? ? ? ? ? ? clear();?
? ? ? ? ? ? for(int i = 0, l = filtered.size(); i < l; i++)?
? ? ? ? ? ? ? ? add(filtered.get(i));?
? ? ? ? ? ? notifyDataSetInvalidated();?
? ? ? ? }?
?
? ? }?
}?
http://appvisum.com/Alxandr/MyMir.

读书人网 >移动开发

热点推荐