Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用自定义ArrayAdapter进行筛选_Android_Listview_Filter_Android Arrayadapter - Fatal编程技术网

Android 使用自定义ArrayAdapter进行筛选

Android 使用自定义ArrayAdapter进行筛选,android,listview,filter,android-arrayadapter,Android,Listview,Filter,Android Arrayadapter,我已经为此挣扎了一段时间,尽管寻找了一段时间,还是找不到答案。我有一个自定义arrayAdapter,它用自定义对象POI填充我的列表视图 package com.WasserSportLotse; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGr

我已经为此挣扎了一段时间,尽管寻找了一段时间,还是找不到答案。我有一个自定义arrayAdapter,它用自定义对象POI填充我的列表视图

package com.WasserSportLotse;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;

class poiAdapter extends ArrayAdapter<POI>{


    private ArrayList<POI> items;
    private Context mContext;
    public ArrayList<POI> allItems;
    private Filter filter;
    public ArrayList<POI> filtered;

    public poiAdapter(Context context, int textViewResourceId, ArrayList<POI> items){
        super(context, textViewResourceId, items);
        mContext = context;
        this.items = items;


}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_items, null);
            }
            POI poi = items.get(position);
            if (poi != null) {
                    TextView mDistanceTextView = (TextView) v.findViewById(R.id.listitems_distancetxt);
                    TextView mNameTextView = (TextView) v.findViewById(R.id.listitems_nametxt);
                    TextView mCategoryTextView = (TextView) v.findViewById(R.id.listitems_categorytxt);
                    if (mDistanceTextView != null) {
                        mDistanceTextView.setText(poi.getDistance()+"km");                            
                        }
                    if(mNameTextView != null){
                        mNameTextView.setText(poi.getName());
                    }
                    if(mCategoryTextView != null){
                        mCategoryTextView.setText(poi.getType());
                        setImage(poi.getType(), v);

                    }
            }
            return v;
    }

    private void setImage(String type, View v) {
        if ("shopping".equals(type)){
            ImageView mImage = (ImageView) v.findViewById(R.id.imageView1);
            mImage.setImageResource(R.drawable.shopping);
        }
        if ("liegestelle".equals(type)){
            ImageView mImage = (ImageView) v.findViewById(R.id.imageView1);
            mImage.setImageResource(R.drawable.liegestelle);
        }
        if ("restaurant".equals(type)){
            ImageView mImage = (ImageView) v.findViewById(R.id.imageView1);
            mImage.setImageResource(R.drawable.restaurant);
        }
        if ("schleuse".equals(type)){
            ImageView mImage = (ImageView) v.findViewById(R.id.imageView1);
            mImage.setImageResource(R.drawable.schleuse);
        }
        if ("verein".equals(type)){
            ImageView mImage = (ImageView) v.findViewById(R.id.imageView1);
            mImage.setImageResource(R.drawable.verein);
        }
        if ("tankstelle".equals(type)){
            ImageView mImage = (ImageView) v.findViewById(R.id.imageView1);
            mImage.setImageResource(R.drawable.tankstelle);
        }
        if ("faekalien".equals(type)){
            ImageView mImage = (ImageView) v.findViewById(R.id.imageView1);
            mImage.setImageResource(R.drawable.faekalien);
        }
        if ("werkstatt".equals(type)){
            ImageView mImage = (ImageView) v.findViewById(R.id.imageView1);
            mImage.setImageResource(R.drawable.werkstatt);
        }
        if ("uebernachtung".equals(type)){
            ImageView mImage = (ImageView) v.findViewById(R.id.imageView1);
            mImage.setImageResource(R.drawable.uebernachtung);
        }

    }



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

    private class POITypeFilter extends Filter
    {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // NOTE: this function is *always* called from a background thread, and
            // not the UI thread.
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if(constraint != null && constraint.toString().length() > 0)
            {
                ArrayList<POI> filt = new ArrayList<POI>();
                ArrayList<POI> lItems = new ArrayList<POI>();
                synchronized (this)
                {
                    lItems.addAll(items);
                }
                for(int i = 0, l = lItems.size(); i < l; i++)
                {
                    POI m = lItems.get(i);
                    if(m.getType().toLowerCase().contains(constraint))
                        filt.add(m);
                }
                result.count = filt.size();
                result.values = filt;
            }
            else
            {
                synchronized(this)
                {
                    result.values = items;
                    result.count = items.size();
                }
            }
            return result;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // NOTE: this function is *always* called from the UI thread.
            filtered = (ArrayList<POI>)results.values;
            notifyDataSetChanged();
            clear();
            for(int i = 0, l = filtered.size(); i < l; i++)
                add(filtered.get(i));
            notifyDataSetInvalidated();
        }

    }


}
这只会返回一个黑屏,就好像所有内容都被过滤掉了一样。我在getFilter上设置了断点,但应用程序确实进入了它,所以我不确定应用程序是否使用了此代码。有什么明显的东西我遗漏了吗

更新-中途

我这里有几个问题。看起来应用程序没有调用getFilter作为断点,而不是停止在那里。事实上,Davlik虚拟机只能使用这么多断点(记在心里的断点并不多)。它在我使用我想要分析的代码时停止使用断点。我觉得我很不走运

那代码哪里出了问题,我改了

 private ArrayList<POI> items; 
私有数组列表项;

公开。因此,它可以被读入一个新的数组并过滤结果。现在,我想知道如何使arrayAdapter再次在原始阵列上进行过滤。哇,这是一个很长的过程。

如果你想定制一个过滤器,你所要做的就是在你的POI类上创建一个toString()方法,它返回你想要过滤的值

 private ArrayList<POI> items;