listview中的EditText搜索项不起作用

时间:2013-10-23 14:32:34

标签: java android search android-listview android-edittext

大家好我在listview中实现了一个edittext来过滤这些项目。我正在关注this示例,但没有任何反应。它不起作用,当我尝试过滤它不做我想要的..这是我的edittext代码的一部分:

ListView lv = (ListView) findViewById(android.R.id.list);
        lv.setTextFilterEnabled(true);
        lv.setAdapter(listadaptor);
        inputSearch.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.this.listadaptor.getFilter().filter(cs);   
            }

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

            }

            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }
        });
    };

我的列表视图是一个列表,显示设备中安装的所有应用程序..我有这个加载列表:

private class LoadApplications extends AsyncTask<Void, Void, Void> {

public ProgressDialog progress = null;

@Override
protected Void doInBackground(Void... params) {
    applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
    listadaptor = new ApplicationAdapter(MainActivity.this,R.layout.snippet_list_row, applist);

    return null;
}

@Override
protected void onCancelled() {
    super.onCancelled();
}

protected void onDestroy() {
    if(progress!=null)
        if(progress.isShowing()){
        progress.dismiss();
        }

}

@Override
protected void onPostExecute(Void result) {
    setListAdapter(listadaptor);
    progress.dismiss();
    super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
    progress = ProgressDialog.show(MainActivity.this, null,
            "Caricamento applicazioni in corso...");
    super.onPreExecute();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

}

所以listadaptor是我的适配器..有人可以帮助我吗?感谢

2 个答案:

答案 0 :(得分:0)

试试这个..

        ArrayList<String> type_name_copy = new ArrayList<String>();
       inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void afterTextChanged(Editable theWatchedText) {
            ArrayList<String> type_name_filter = new ArrayList<String>();

            String text = theWatchedText.toString();

            for (int i = 0; i < array.size(); i++) {

                if ((array.get(i).toLowerCase()).contains(text
                        .toLowerCase())) {
                    type_name_filter.add(array.get(i));

                }
            }

            type_name_copy = type_name_filter;

            listUpdate(type_name_copy);
        }
    });

}

public void listUpdate(ArrayList<String> data) {
    lv.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, data));

}
}

答案 1 :(得分:0)

我建议您使用AutocompleteTextView,它是带有内置搜索功能的Edittext。