过滤listview数据时强制关闭错误

时间:2012-12-21 11:11:10

标签: android listview filter arraylist

在这里提问之前,我看过this thread。但该线程包含简单字符串数组的示例,而在我的情况下,我使用的是ArrayList>所以我此刻有点困惑。

我的android活动中有多个选择列表视图。我使用listview显示联系人列表,以便用户可以一次选择多个电话号码。但是现在我想为相同的目的添加搜索功能。我想要做的是在顶部放置一个EditText,并在EditText中输入任何字符用户类型,我希望ListView根据用户输入填充过滤数据。我有一个CustomAdapter,它扩展了BaseAdapter和一个名为Contact的POJO类,我将其用于GET&设置联系方式。

登录CAT

12-21 10:58:04.204: W/System.err(1737): TEXT EVENT FIRED
12-21 10:58:04.233: W/System.err(1737): IF TAG HITTED
12-21 10:58:04.373: W/System.err(1737): PUBLISH RESULT
12-21 10:58:05.493: D/dalvikvm(503): GC_CONCURRENT freed 414K, 16% free 3018K/3556K, paused 9ms+6ms, total 144ms
12-21 10:58:10.543: W/System.err(1737): TEXT EVENT FIRED
12-21 10:58:10.573: W/System.err(1737): ELSE TAG HITTED
12-21 10:58:10.683: W/System.err(1737): PUBLISH RESULT
12-21 10:58:15.292: I/CheckinService(641): Preparing to send checkin request
12-21 10:58:15.292: I/EventLogService(641): Accumulating logs since 1356087328635
12-21 10:58:15.413: W/System.err(1737): TEXT EVENT FIRED
12-21 10:58:15.673: W/System.err(1737): IF TAG HITTED
12-21 10:58:15.673: W/System.err(1737): PUBLISH RESULT
12-21 10:58:15.673: D/AndroidRuntime(1737): Shutting down VM
12-21 10:58:15.673: W/dalvikvm(1737): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-21 10:58:15.705: E/AndroidRuntime(1737): FATAL EXCEPTION: main
12-21 10:58:15.705: E/AndroidRuntime(1737): java.lang.NullPointerException
12-21 10:58:15.705: E/AndroidRuntime(1737):     at me.bluetitandev.apps.freesms.utils.MyCustomAdapter.getCount(MyCustomAdapter.java:47)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:801)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:5958)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at me.bluetitandev.apps.freesms.utils.MyCustomAdapter$MyContactFilter.publishResults(MyCustomAdapter.java:140)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at android.os.Looper.loop(Looper.java:137)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-21 10:58:15.705: E/AndroidRuntime(1737):     at dalvik.system.NativeStart.main(Native Method)
12-21 10:58:15.754: W/ActivityManager(296):   Force finishing activity me.bluetitandev.apps.freesms/.SelectRec

CUSTOM ADAPTER CODE

public class MyCustomAdapter extends BaseAdapter implements Filterable {

Context mContext;
private LayoutInflater mInflater;
SparseBooleanArray mSparseBooleanArray;
private ArrayList<Map<String,String>> mAdapData = new ArrayList<Map<String, String>>();

public MyCustomAdapter(Context mContext) {
    mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mSparseBooleanArray = new SparseBooleanArray();
}

public ArrayList<String> getCheckedItems() {
    ArrayList<String> mTempArry = new ArrayList<String>();
    for (int i = 0; i < mAdapData.size(); i++) {
        if (mSparseBooleanArray.get(i)) {
            Map<String, String> map = (Map<String, String>) mAdapData.get(i);
            final String numbr = map.get("Phone").toString();
            mTempArry.add(numbr);
        }
    }
    return mTempArry;
}

public int getCount() {
    return this.mAdapData.size();
}

public void addItem(String paramString1, String paramString2) {
    Map<String, String> NameNumber = new HashMap<String, String>();
    NameNumber.put("Name", paramString1);
    NameNumber.put("Phone", paramString2);
    this.mAdapData.add(NameNumber);
    notifyDataSetChanged();
}

@SuppressWarnings("unchecked")
public Object getItem(int paramInt) {
    return (ArrayList<Map<String, String>>) this.mAdapData.get(paramInt);
}

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

public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup) {

    if (paramView == null) {
        paramView = mInflater.inflate(R.layout.multiplecontactview, null);
    }

    TextView txtName = (TextView) paramView.findViewById(R.id.txtContactName);
    TextView txtNumber = (TextView) paramView.findViewById(R.id.txtContactNumber);
    CheckBox mCheckBox = (CheckBox) paramView.findViewById(R.id.checkBox1);
    mCheckBox.setTag(paramInt);
    mCheckBox.setChecked(mSparseBooleanArray.get(paramInt));
    mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);
    txtName.setTextColor(Color.BLACK);
    txtNumber.setTextColor(Color.BLACK);
    for (int i = 0; i < mAdapData.size(); i++) {
        Map<String, String> map = (Map<String, String>) mAdapData.get(paramInt);
        final String name = map.get("Name").toString();
        final String numbr = map.get("Phone").toString();
        txtName.setText(name);
        txtNumber.setText(numbr);
    }
    return paramView;
}

OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
    }
};

@Override
public Filter getFilter() {
    return new MyContactFilter();
}

private class MyContactFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence cs) {
        FilterResults results = new FilterResults();
        ArrayList<Map<String, String>> filList = new ArrayList<Map<String, String>>();

        if(cs != null && cs.toString().length() > 0) {
            System.err.println("IF TAG HITTED");
            for (int i = 0; i < mAdapData.size(); i++) {
                Map<String, String> map = (Map<String, String>) mAdapData.get(i);
                final String names = map.get("Name").toString();
                final String numbr = map.get("Phone").toString();
                if (names.compareTo(cs.toString()) == 0) {
                    Map<String, String> FilNameNumber = new HashMap<String, String>();
                    FilNameNumber.put("Name", names);
                    FilNameNumber.put("Phone", numbr);
                    filList.add(FilNameNumber);
                }
                results.values = filList;
                results.count = filList.size();
            }
        } else {
            System.err.println("ELSE TAG HITTED");
            synchronized (mAdapData) {
                results.values = mAdapData;
                results.count = mAdapData.size();
            }
        }
        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence cs, FilterResults fr) {
        System.err.println("PUBLISH RESULT");
        mAdapData = (ArrayList<Map<String, String>>) fr.values;
        notifyDataSetChanged();
    }
}
}

2 个答案:

答案 0 :(得分:0)

在您的getCount方法@Override丢失之前,只需添加它并尝试

答案 1 :(得分:0)

我在mAdapData过滤2-3次后得到空值,这导致应用程序崩溃。为了解决这个问题,我在过滤之前创建了原始值的副本,如果没有应用过滤器,我将结果数据传递给结果,解决了错误。