Filter Adapter 1개
package me.alxandr.android.mymir.adapters;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import me.alxandr.android.mymir.R;
import me.alxandr.android.mymir.model.Manga;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.SectionIndexer;
import android.widget.TextView;
public class MangaListAdapter extends ArrayAdapter<Manga> implements SectionIndexer
{
public ArrayList<Manga> items;
public ArrayList<Manga> filtered;
private Context context;
private HashMap<String, Integer> alphaIndexer;
private String[] sections = new String[0];
private Filter filter;
private boolean enableSections;
public Manga getByPosition(int position)
{
return items.get(position);
}
public MangaListAdapter(Context context, int textViewResourceId, ArrayList<Manga> items, boolean enableSections)
{
super(context, textViewResourceId, items);
this.filtered = items;
this.items = filtered;
this.context = context;
this.filter = new MangaNameFilter();
this.enableSections = enableSections;
if(enableSections)
{
alphaIndexer = new HashMap<String, Integer>();
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);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if(v == null)
{
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mangarow, null);
}
Manga o = items.get(position);
if(o != null)
{
TextView tt = (TextView) v.findViewById(R.id.MangaRow_MangaName);
TextView bt = (TextView) v.findViewById(R.id.MangaRow_MangaExtra);
if(tt != null)
tt.setText(o.getName());
if(bt != null)
bt.setText(o.getLastUpdated() + " - " + 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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access Request, Session and Application in Struts2If we want to use request, Session and application in JSP, what should we do? We can obtain Map type objects such as Req...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.