0

我正在按照 android hive 教程制作自定义列表视图 + 可搜索列表视图。这些教程很容易理解,但是当我在 Lazyadapter 添加 getFilter() 时,问题就出现了。seach 编辑框可以过滤列表视图,但在清除文本视图时,列表视图的原始数据未显示。

这是我的代码

 package com.example.androidhive;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
        TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        title.setText(song.get(CustomizedListView.KEY_TITLE));
        artist.setText(song.get(CustomizedListView.KEY_ARTIST));
        duration.setText(song.get(CustomizedListView.KEY_DURATION));
        imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
        return vi;
    }

    public android.widget.Filter getFilter() {
        // TODO Auto-generated method stub
        return new android.widget.Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults Result = new FilterResults();
                // if constraint is empty return the original names
                if (TextUtils.isEmpty(constraint)){
                    //Result.values = storedata;
                    Result.count = data.size();
                    Result.values = data;
                    return Result;
                }

                ArrayList<HashMap<String, String>> Filtered_Names = new ArrayList<HashMap<String, String>>();
                String filterString = constraint.toString().toLowerCase();
                String filterableString;

                for(int i = 0; i<data.size(); i++){
                    HashMap<String,String> searchdata = data.get(i);
                    String itemsearched = searchdata.get(CustomizedListView.KEY_TITLE);

                    filterableString = itemsearched;
                    if(filterableString.toLowerCase().contains(filterString)){
                    Filtered_Names.add(data.get(i));
                    Log.e("Added", String.valueOf(Filtered_Names.size()));
                    }

                    }
                Result.values = Filtered_Names;
                Result.count = Filtered_Names.size();
                Log.e("Results", Result.values.toString() + String.valueOf(Result.count));
                return Result;
            }

            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
                // TODO Auto-generated method stub
                    ArrayList<HashMap<String, String>> resultList = (ArrayList<HashMap<String, String>>) results.values;
                    LazyAdapter.this.data = resultList;
                    LazyAdapter.this.notifyDataSetChanged();
            }

        };
    }
}
4

1 回答 1

1

这一行LazyAdapter.this.data = resultList;实际上改变了数据的值,所以当你进入空约束部分时,你只是没有修改当前的过滤。

保留对第一个数据集的引用:

  1. 声明一个private ArrayList<HashMap<String, String>> origData;
  2. 在构造函数中影响它:origData = d;
  3. 在空约束情况的过滤器中使用它

如下

Result.count = origData.size();
Result.values = origData;
于 2013-07-11T08:43:31.513 回答