【发布时间】:2014-04-25 13:48:09
【问题描述】:
我正在尝试在我的自定义列表视图中进行搜索。但是当 EditText 中的文本发生变化时,IF 不起作用。那是我的代码。我使用简单适配器。怎么了?
editText.addTextChangedListener(new TextWatcher() {
@Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { //get the text in the EditText String searchString= editText.getText().toString(); int textLength=searchString.length(); //clear the initial data set searchResults.clear(); for(int i=0;i<docs.size();i++) { String playerName=docs.get(i).get(FIRST).toString(); if(textLength<=playerName.length()){ //compare the String in EditText with Names in the ArrayList if(searchString.equalsIgnoreCase(playerName.substring(0,textLength))) { searchResults.add(docs.get(i)); Log.i("searchResults", docs.get(i)+""); } else Log.i("doesn't work", "doesn't work"); } } adapter.notifyDataSetChanged(); adapter = new SimpleAdapter(Main.this, searchResults, R.layout.list, new String[] { FIRST, LAST, DATE }, new int[] { R.id.text1, R.id.text2, R.id.date }); listView.setAdapter(adapter); listView.setTextFilterEnabled(true); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } });
【问题讨论】:
-
你能解释一下,“不起作用”吗?
-
@codeMagic 当我在editText中输入文本进行搜索时,我只有一个空的listView。
-
@codeMagic 我的 IF 不起作用,当我在 EditText 中输入值时,它等于我的 ArrayList 中的值。
-
你为什么打电话给
notifyDataSetChanged()并且每次都重新初始化你的Adapter? -
@codeMagic,每次editText中的文本更改后,我都必须显示结果。
标签: java android search simpleadapter