0

在这里问问题之前,我看了一下这个线程。但是该线程包含简单字符串数组的示例,而在我的情况下,我使用的是 ArrayList> 所以我现在有点困惑。

我的 android 活动中有一个多选列表视图。我正在使用列表视图来显示联系人列表,以便用户一次可以选择多个电话号码。但现在我想为同样的目的添加搜索功能。我想要做的是在顶部放置一个 EditText,无论用户在该 EditText 中键入什么字符,我都希望 ListView 填充基于用户输入的过滤数据。我有一个扩展 BaseAdapter 的 CustomAdapter 和一个名为 Contact 的 POJO 类,我用它来获取和设置联系人详细信息。

日志猫

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

自定义适配器代码

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

2 回答 2

0

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

于 2012-12-21T11:19:27.780 回答
0

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

于 2012-12-23T04:26:02.283 回答