0

我在我的 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;
}

}

但是,当我启动应用程序并开始过滤时,每个列表条目都有正确的图片,但名称只是原始列表,截断为实际满足过滤器的名人数量。假设 Kirsten Dunst 是列表中的第一个条目,Adam Savage 是第二个。如果我过滤 Adam Savage,我会得到他的照片,但名称仍然是 Kirsten Dunst,尽管这两条信息是单个对象的元素。

显然,这不是我们想要的结果。想法?

4

1 回答 1

1

我不确定您是如何使用适配器的,所以我将向您展示我如何使用延迟加载过滤 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 中,根据字符串将名人过滤到数组 filtersItems 中,然后设置 filters=true 并创建一个新的 SlowAdapter 并将其设置为您的 ListView。

unfilteredItems 将在未过滤任何内容时使用,并将用于将来从完整源进行过滤。

于 2013-04-22T23:52:45.993 回答