过滤ListView与自定义Array适配器(Android)



我已经在ListView上实现了一个过滤器,它建立在一个自定义数组适配器上。该列表显示名人的名字和该名人的照片。

public class Celebrities extends ListActivity {
private EditText filterText = null;
ArrayAdapter<CelebrityEntry> adapter = null;
private TextWatcher filterTextWatcher = new TextWatcher() {
    public void afterTextChanged(Editable s) {
    }
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        adapter.getFilter().filter(s);
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_celebrity);
     //disables the up button
     getActionBar().setDisplayHomeAsUpEnabled(true);
     filterText = (EditText) findViewById(R.id.search_box);
     filterText.addTextChangedListener(filterTextWatcher);
     adapter = new CelebrityEntryAdapter(this, getModel());
     setListAdapter(adapter); 
}

我已经覆盖了toString()方法在CelebrityEntry.java:

public final class CelebrityEntry {
private String name;
private int pic;
public CelebrityEntry(String name, int pic) {
    this.name = name;
    this.pic = pic;
}
/**
 * @return name of celebrity
 */
public String getName() {
    return name;
}
/**
 * override the toString function so filter will work
 */
public String toString() {
    return name;
}
/**
 * @return picture of celebrity
 */
public int getPic() {
    return pic;
}

}

然而,当我启动应用程序并开始过滤时,每个列表条目都有适当的图片,但名称只是原始列表,被截断为实际满足过滤器的名人数量。假设克尔斯滕·邓斯特是名单上的第一名,亚当·萨维奇是第二名。如果我过滤Adam Savage,我得到的是他的照片,但名字仍然是Kirsten Dunst,尽管这两条信息是一个对象的元素。

显然,这不是期望的结果。想法吗?

我不确定你是如何使用你的适配器,所以我将向你展示我做什么过滤ListView与延迟加载(这将回收行视图,而不是膨胀的新视图,因为你滚动)。创建一个SlowAdapter内部类:

private class SlowAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    public SlowAdapter(Context context) {
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    public int getCount() {
        if (filtered) {
            return filteredItems.length;
        } else {
            return unfilteredItems.length;
        }
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout rowView;
        if (convertView == null) {
            rowView = (LinearLayout)mInflater.inflate(R.layout.row, parent, false);
        } else {
            rowView = (LinearLayout)convertView;
        }
        ImageView celebrity_image = rowView.findViewById(R.id.celebrity_image);
        TextView celebrity_name = rowView.findViewById(R.id.celebrity_name);
        if (!filtered) { // use position to get the filtered item.
            CelebrityEntry c = filteredItems[position];
             // do what you do to set the image and text for a celebrity.
        } else { // use position to get the unfiltered item.
            CelebrityEntry c = unfilteredItems[position];
             // do what you do to set the image and text for a celebrity.                 
        }   
        return rowView;
    }
}

现在在你的textWatcher中,根据字符串过滤名人到数组filteredItems中,然后设置filtered=true并创建一个新的SlowAdapter并将其设置为ListView。

unfilteredItems将在没有过滤时使用,它将用于将来从完整源中过滤。

最新更新