将过滤器应用于ListView的单个项目

时间:2014-07-19 11:11:57

标签: android listview

我想在我的ListView上实现一个搜索栏。 我使用的是SimpleAdapter,代码如下:

adapter = new SimpleAdapter(this, menuItems,
                        R.layout.list_item,
                        new String[] { KEY_NAME, KEY_DESC, KEY_ADDRESS, KEY_LAT, KEY_LNG, KEY_IDLOC, KEY_DATE, KEY_IDPR}, new int[] {
                                R.id.name, R.id.desciption, R.id.cost, R.id.latitude, R.id.longitude, R.id.id, R.id.date, R.id.pr});

我只会为 KEY_ADDRESS 项应用搜索栏。我该怎么办?

编辑:我添加我的活动

 ListAdapter adapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab3main);



    ParserTask pt=new ParserTask();
    pt.execute();

    ArrayList<HashMap<String, String>> menuItems;

        try {
            menuItems = pt.get();
             adapter = new SimpleAdapter(this, menuItems,
                        R.layout.list_item,
                        new String[] { KEY_NAME, KEY_DESC, KEY_ADDRESS, KEY_LAT, KEY_LNG, KEY_IDLOC, KEY_DATE, KEY_IDPR}, new int[] {
                                R.id.name, R.id.desciption, R.id.cost, R.id.latitude, R.id.longitude, R.id.id, R.id.date, R.id.pr});

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

我尝试过:

final EditText myFilter = (EditText) findViewById(R.id.myFilter);
        myFilter.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                String text = myFilter.getText().toString().toLowerCase(Locale.getDefault());
                ((SimpleAdapter) adapter).getFilter().filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

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

            }

但它返回一个NUllPointerException。 Logcat下面:

  07-18 18:40:44.536: E/AndroidRuntime(274): FATAL EXCEPTION: main

07-18 18:40:44.536:E / AndroidRuntime(274):java.lang.NullPointerException 07-18 18:40:44.536:E / AndroidRuntime(274):在android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93) 07-18 18:40:44.536:E / AndroidRuntime(274):在android.widget.AdapterView.checkFocus(AdapterView.java:689) 07-18 18:40:44.536:E / AndroidRuntime(274):在android.widget.AdapterView $ AdapterDataSetObserver.onInvalidated(AdapterView.java:813) 07-18 18:40:44.536:E / AndroidRuntime(274):在android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:43) 07-18 18:40:44.536:E / AndroidRuntime(274):在android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:54) 07-18 18:40:44.536:E / AndroidRuntime(274):在android.widget.SimpleAdapter $ SimpleFilter.publishResults(SimpleAdapter.java:383) 07-18 18:40:44.536:E / AndroidRuntime(274):在android.widget.Filter $ ResultsHandler.handleMessage(Filter.java:282) 07-18 18:40:44.536:E / AndroidRuntime(274):在android.os.Handler.dispatchMessage(Handler.java:99) 07-18 18:40:44.536:E / AndroidRuntime(274):在android.os.Looper.loop(Looper.java:123) 07-18 18:40:44.536:E / AndroidRuntime(274):在android.app.ActivityThread.main(ActivityThread.java:4627) 07-18 18:40:44.536:E / AndroidRuntime(274):at java.lang.reflect.Method.invokeNative(Native Method) 07-18 18:40:44.536:E / AndroidRuntime(274):at java.lang.reflect.Method.invoke(Method.java:521) 07-18 18:40:44.536:E / AndroidRuntime(274):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 07-18 18:40:44.536:E / AndroidRuntime(274):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-18 18:40:44.536:E / AndroidRuntime(274):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您需要为它编写自定义适配器。

点击此链接 - http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/