2012-09-11 109 views
1

我已经实现了从this tutorial的CustomArrayAdapter有一个缩略图和2行列表视图的每个条目。除了过滤之外,它完美的工作。我一直在尝试执行thisthisthis问题, 之后的过滤,但是listview永远不会更新。自定义ArrayAdapter过滤时未更新

这是我对多布局代码:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="?android:attr/listPreferredItemHeight" 
android:padding="6dip"> 

<ImageView 
    android:id="@+id/thumb" 
    android:background="@drawable/thumb" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_marginRight="6dip" 
    android:layout_marginTop="2dip" 
    android:layout_marginBottom="2dip"/> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="0dip" 
    android:layout_weight="1" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:id="@+id/custom_title" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:textColor="@color/black" 
     android:gravity="center_vertical" 
     /> 

    <TextView 
     android:id="@+id/desc" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:textColor="@color/darkgray" 
     android:textStyle="italic" 
     android:singleLine="true" 
     android:ellipsize="marquee" 
     /> 

</LinearLayout> 

,这是我的代码为我customListAdapter(继first link

public class CustomListAdapter extends ArrayAdapter implements Filterable{ 

private static ArrayList<CustomListItem> searchArrayList; 
private static ArrayList<CustomListItem> subItems; 
private PTypeFilter filter; 


private LayoutInflater mInflater; 

public CustomListAdapter(Context context, int textViewResourceId, ArrayList<CustomListItem> results) { 
    super(context,textViewResourceId, results); 
    searchArrayList = results; 
    mInflater = LayoutInflater.from(context); 
} 
public int getCount() { 
     return searchArrayList.size(); 
    } 

    public Object getItem(int position) { 
     return searchArrayList.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.custom_row, null); 
     holder = new ViewHolder(); 
     holder.txtTitle = (TextView) convertView.findViewById(R.id.custom_title); 
     holder.txtDesc = (TextView) convertView.findViewById(R.id.desc); 
     holder.thumb = (ImageView) convertView.findViewById(R.id.thumb); 

     convertView.setTag(holder); 
     } else { 
     holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.txtTitle.setText(searchArrayList.get(position).getTitle()); 
     holder.txtDesc.setText(searchArrayList.get(position).getDesc()); 
     holder.thumb.setImageBitmap(searchArrayList.get(position).getThumb()); 
     //holder.thumb.setBackgroundColor(Color.BLACK); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView txtTitle; 
     TextView txtDesc; 
     ImageView thumb; 
    }  

    @Override 
    public Filter getFilter() { 
     Log.i("CustomListAdapter", "testing filter"); 
     if (filter == null){ 
      filter = new PTypeFilter(); 
     } 
     return filter; 
     } 

    private class PTypeFilter extends Filter{ 


@SuppressWarnings("unchecked") 
@Override 
protected void publishResults(CharSequence prefix, 
           FilterResults results) { 
    // NOTE: this function is *always* called from the UI thread. 
    subItems = (ArrayList<CustomListItem>)results.values; 

    notifyDataSetChanged(); 
} 

@SuppressWarnings("unchecked") 
protected FilterResults performFiltering(CharSequence prefix) { 
     // NOTE: this function is *always* called from a background thread, and 
     // not the UI thread. 

     FilterResults results = new FilterResults(); 
     ArrayList<CustomListItem> i = new ArrayList<CustomListItem>(); 

     if (prefix!= null && prefix.toString().length() > 0) { 

      for (int index = 0; index < searchArrayList.size(); index++) { 
       CustomListItem si = searchArrayList.get(index); 
       if(si.toString().compareTo(prefix.toString()) == 0){ 
       i.add(si); 
       } 
      } 
      results.values = i; 
      results.count = i.size();     
     } 
     else{ 
      synchronized (searchArrayList){ 
       results.values = searchArrayList; 
       results.count = searchArrayList.size(); 
      } 
     } 

     return results; 
    } 
}  
} 

而且从我的主要活动我请拨打这样的自定义基本适配器:

adapter = new CustomListAdapter(this, R.id.custom_title, DATA); 
setListAdapter(adapter); 

(不是100%确定R.id.custom_title是我应该放在那里的ID。

我添加了调试消息,并且正在调用TextChange作为getFilter()方法。 所以我不知道我在做什么错。之前我从ListAdapter到CustomListAdapter转换,滤波是工作的罚款:(

感谢您的帮助

+0

难道你重写'CustomListItem'类的'toString'方法来返回一些有意义的东西吗? – Luksprog

+0

是的,我做了,我返回标题+描述 – marimaf

+0

http://stackoverflow.com/questions/8678163/list-filter-custom-adapter-不要给结果/ 8678198#8678198 –

回答

2

ListView没有被过滤,因为你设置未在适配器使用的过滤上subItems结果类,所以对notifyDataSetChanged的调用将不起作用,因为从适配器的角度来看,数据是完好无损的。使用另一个列表来保存旧值是正确的,它们将是必需的,以便您拥有所有旧值。该问题添加该行:

subItems = new ArrayList<CustomListItem>(searchArrayList); 

in该适配器的构造函数,这是因为你有一个完整的值的副本,以供日后使用。然后在publishResults方法使用searchArrayList名单,因为这是备份适配器列表:

searchArrayList = (ArrayList<CustomListItem>)results.values; 
notifyDataSetChanged(); 

然后在performFiltering方法:

if (prefix!= null && prefix.toString().length() > 0) { 
     // use the initial values !!! 
     for (int index = 0; index < subItems.size(); index++) { 
      CustomListItem si = subItems.get(index); 
      final int length = prefix.length(); 
      // if you compare the Strings like you did it will never work as you compare the full item string(you'll have a match only when you write the EXACT word) 
      // keep in mind that you take in consideration capital letters! 
      if(si.toString().substring(0, prefixLength).compareTo(prefix.toString()) == 0){ 
      i.add(si); 
      } 
     } 
     results.values = i; 
     results.count = i.size();     
    } 
    else{ 
     // revert to the old values 
     synchronized (searchArrayList){ 
      results.values = subItems; 
      results.count = subItems.size(); 
     } 
    } 

我希望它的作品。

(不是100%肯定R.id.custom_title是我应该把 那里的ID。

你用你自己的自定义适配器没关系。

+0

哇,非常感谢!它完美的作品 – marimaf