清除可搜索的列表视图

时间:2013-07-11 08:32:11

标签: android android-listview

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

这是我的代码

 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();
            }

        };
    }
}

1 个答案:

答案 0 :(得分:1)

此行LazyAdapter.this.data = resultList;实际上更改了数据的值,因此当您进入空约束部分时,您只是不修改当前过滤。

通过以下方式保留对第一个数据集的引用:

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

    Result.count = origData.size();
    Result.values = origData;