这是我的代码:
public class ListAdapter extends BaseAdapter implements Filterable{
private List<Item> filteredData;
private Context _context;
private List<String> list;
ViewHolder holder=null;
View row=null;
private ItemFilter mFilter = new ItemFilter();
private LayoutInflater mInflater;
private List<Item> items;
/*public ListAdapter(Context _context,List<String> list,List<Item> items)
{
this._context=_context;
this.list=list;
this.items=items;
}*/
public ListAdapter(Context _context,List<String> list)
{
this._context=_context;
this.list=list;
mInflater = LayoutInflater.from(_context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
private static class ViewHolder
{
public TextView textView;
public ImageView imageThumbnail;
public ViewHolder(View row) {
// TODO Auto-generated constructor stub
textView = (TextView) row.findViewById(R.id.rowTextView);
}
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
// TODO Auto-generated method stub
row=convertView;
holder=null;
// Item i = items.get(position);
try{
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.simplerow, viewGroup, false);
holder=new ViewHolder(row);
row.setTag(holder);
Log.d("newrow", "New row");
/*/View row = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.simplerow, viewGroup, false);
}
TextView textView = (TextView) row.findViewById(R.id.rowTextView);*/
holder.textView.setText(list.get(position).toString());
/* if(holder.imageThumbnail != null)
{
Bitmap myBitmap = BitmapFactory.decodeFile(path);
if(myBitmap != null)
holder.imageThumbnail.setImageBitmap(myBitmap);
}*/
/* bmThumbnail = ThumbnailUtils.createVideoThumbnail(Thumbnails.MICRO_KIND);*/
/*ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
bmThumbnail = MediaStore.Video.Thumbnails.getThumbnail(crThumb, position, MediaStore.Video.Thumbnails.MICRO_KIND, options);*/
}
else
{
holder= (ViewHolder) row.getTag();
Log.d("recycle", "Recycling stuff");
}
}
catch(Exception e)
{
e.printStackTrace();
}
/* try
{
if(row!=null)
{
if(position==MP4.selected_item)
{
row.setBackgroundColor(Color.RED);
}
else
{
row.setBackgroundColor(Color.BLACK);
}
}
}
catch(Exception e)
{
}
*/
return row;
}
/*public Object getFilter() {
// TODO Auto-generated method stub
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
if (charSequence == null || charSequence.length() == 0) {
results.values = items;
results.count = items.size();
} else {
List<Item> filtered_items = new ArrayList<Item>();
for (int i = 0; i < items.size(); i++) {
if (items.get(i).getText().toString().contains(charSequence))
filtered_items.add(items.get(i));
}
results.values = filtered_items;
results.count = filtered_items.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredData = (List<Item>) filterResults.values;
notifyDataSetChanged();
}
};
}*/
public Filter getFilter() {
// TODO Auto-generated method stub
/*if (mFliter == null)
mFliter = new CustomFilter();*/
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (List<Item>) results.values;
notifyDataSetChanged();
}
}
}
ListAdapter.java
{{1}}
我正在尝试在此列表视图中使用过滤搜索,但是当我键入搜索时,它不返回任何内容,但它应显示以列表视图中键入的字母开头或包含的字母。 Listview根本不更新,并保存listview中显示的项目。我做错了什么,我该怎么办?
答案 0 :(得分:0)
要使代码正常工作,您必须更改适配器连接到ArrayList
的{{1}}。您可以使用ListView
方法执行此操作:
publishResults()
(请注意,您的列表被声明为“String”列表,而不是“Item”。) 如何更新“列表”取决于您通常执行的过滤操作类型。最好只运行列表,并对“list”中的每个项目检查它是否包含在结果列表中,如果是,请保留它,如果没有则删除它。
编写一种“逆过滤器”甚至可能更好(性能),“filteredData”包含不需要的项目,然后运行它们并调用protected void publishResults(CharSequence constraint, FilterResults results)
{
// no selection - nothing to do :)
if (results.count == 0)
return;
// you wrote the filter so that this list contains
// the values you want to keep:
filteredData = (List<String>) results.values;
// now remove all the other values from the adapter list:
// for example:
list.clear();
for (String item : filteredData)
{
list.add(item);
}
// now your adapter data list contains only the filter results, so
notifyDataSetChanged();
}
。
我在评论中提到的另一件事:我想保留原始数据,以便能够重置过滤器。为此,我在适配器中引入了一个成员变量
list.remove()
并将以下代码添加到List<String> mOriginalData;
:
getFilter()
稍后在过滤过程中,某种选择(约束=“”)是重置过滤器的信号,即通过将原始数据复制回(先前清除的)适配器数据列表来显示原始数据。