可过滤的结果没有在android中显示

时间:2015-01-16 05:10:22

标签: android

活动类

这是我的适配器对象:

mAdapter = new SampleAdapter(this,android.R.layout.simple_list_item_1,shareList);

这是我的班级从网上填充数据的地方。

private void onLoadMoreItems(ArrayList<Share> shareList) {          
    //this.shareList=shareList;
    Log.d("size of temo",Integer.toString(shareList.size()));
    for (Share share : shareList) {
       // mAdapter.add(share);
        System.out.println("Image Url : "+share.item.getMiddleImageUrl());
        this.shareList.add(share);
        Log.v("item", share.item.title);
    }

    Log.d("size of temo",Integer.toString(sampleData.size()));


    //mAdapter.notifyDataSetChanged();
    mHasRequestedMore = false;
   // mGridView.setAdapter(mAdapter);
    mGridView.setAdapter(mAdapter);
    mGridView.setOnScrollListener(this);
    mGridView.setOnItemClickListener(this);
}

我的edittext的动作侦听器:

/** Setting an action listener */
txtSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        Log.v("msg", s.toString());
        HomeActivity.this.mAdapter.getFilter().filter(s.toString());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});

适配器类

public class SampleAdapter extends ArrayAdapter<Share> implements Filterable {

    private static final String TAG = "SampleAdapter";

    private final LayoutInflater mLayoutInflater;
    private final Random mRandom;
    private ArrayList<Share> share;
    private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();

    private ModelFilter filter;
    private List<Share> allModelItemsArray;
    private List<Share> filteredModelItemsArray;


    public SampleAdapter(Context context, int textViewResourceId,ArrayList<Share> share) {
        super(context, textViewResourceId,share);
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mRandom = new Random();

        this.allModelItemsArray = new ArrayList<Share>();
        allModelItemsArray.addAll(share);
        this.filteredModelItemsArray = new ArrayList<Share>();
        filteredModelItemsArray.addAll(allModelItemsArray);
    }

    @Override
    public View getView(final int position, View convertView,
            final ViewGroup parent) {

        ViewHolder vh;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.row_grid_item,
                    parent, false);
            vh = new ViewHolder();
            vh.imgView = (DynamicHeightImageView) convertView
                    .findViewById(R.id.imgView);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        double positionHeight = getPositionRatio(position);

        vh.imgView.setHeightRatio(positionHeight);

        //DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder();

        ImageLoader.getInstance().displayImage(getItem(position).item.getMiddleImageUrl(), vh.imgView);
        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (filter == null){
          filter  = new ModelFilter();
        }
        return filter;
      }

    static class ViewHolder {
        DynamicHeightImageView imgView;
    }

    private double getPositionRatio(final int position) {
        double ratio = sPositionHeightRatios.get(position, 0.0);
        // if not yet done generate and stash the columns height
        // in our real world scenario this will be determined by
        // some match based on the known height and width of the image
        // and maybe a helpful way to get the column height!
        if (ratio == 0) {
            ratio = getRandomHeightRatio();
            sPositionHeightRatios.append(position, ratio);
            Log.d(TAG, "getPositionRatio:" + position + " ratio:" + ratio);
        }
        return ratio;
    }

    private double getRandomHeightRatio() {
        return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5
                                                    // the width
    }

private class ModelFilter extends Filter
{    
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        constraint = constraint.toString().toLowerCase();
        FilterResults result = new FilterResults();
        if(constraint != null && constraint.toString().length() > 0)
        {
            ArrayList<Share> filteredItems = new ArrayList<Share>();
            System.out.println(allModelItemsArray.size());
            for(int i = 0, l = allModelItemsArray.size(); i < l; i++)
            {
                Share m = allModelItemsArray.get(i);
                if(m.item.title.toLowerCase().contains(constraint))
                    Log.v("hereeeeeeeeeeeeeeeeee", m.item.title);
                    filteredItems.add(m);
            }
            result.count = filteredItems.size();
            result.values = filteredItems;
        }
        else
        {
            synchronized(this)
            {
                result.values = allModelItemsArray;
                result.count = allModelItemsArray.size();
            }
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        filteredModelItemsArray = (ArrayList<Share>)results.values;
        notifyDataSetChanged();
        clear();
        for(int i = 0, l = filteredModelItemsArray.size(); i < l; i++)
            add(filteredModelItemsArray.get(i));
        notifyDataSetInvalidated();
    }
}

文本观察器工作正常,但在我的执行中,过滤allModelItemsArray.size()给出了0.
不知道我做错了什么。

0 个答案:

没有答案