使用自定义数组适配器(Android)过滤ListView

时间:2013-04-22 23:17:59

标签: android listview

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

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

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,尽管这两条信息是单个物体的元素。

显然,这不是理想的结果。想法?

1 个答案:

答案 0 :(得分: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中,根据字符串将名人过滤到数组filteredItems中,然后设置filtered = true并创建一个新的SlowAdapter并将其设置为ListView。

unfilteredItems将在没有过滤任何内容时使用,并且将用于将来从完整源过滤。