过滤时,自定义ArrayAdapter不会更新

时间:2012-09-11 03:03:51

标签: android android-listview listadapter

我已经从this tutorial实现了一个CustomArrayAdapter,以便为列表视图的每个条目添加一个缩略图和2行。除了过滤之外,它的工作原理完美。我一直在尝试在thisthisthis问题后实施过滤,  但列表视图永远不会更新。

这是我的多行布局代码:

<?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是我应该放在那里的身份。

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

谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

ListView未过滤,因为您在subItems上设置了过滤器的结果,但未在适配器类中使用,因此对notifyDataSetChanged的调用将不起作用适配器的观点数据是完整的。你使用另一个列表保存旧值是正确的,它们将是必需的,因此你可以拥有所有旧值。要解决问题,请添加以下行:

subItems = new ArrayList<CustomListItem>(searchArrayList);

在适配器的构造函数中,这样您就可以获得稍后要使用的完整值的副本。然后在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   那里。

使用自己的自定义适配器无关紧要。