1

我已经从本教程中实现了一个 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 代码(在第一个链接之后)

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 之前,过滤工作正常:(

感谢您的帮助

4

1 回答 1

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。

这并不重要,因为您使用自己的自定义适配器。

于 2012-09-11T04:04:01.583 回答