使用SimpleAdapter在Custom ListView中搜索

时间:2014-04-25 13:48:10

标签: java android search simpleadapter

我正在尝试在自定义列表视图中执行搜索。但是当EditText中的文本更改时,IF不起作用。那是我的代码。我使用SimpleAdapter。有什么问题?

  

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    

} });

1 个答案:

答案 0 :(得分:0)

这是我在listview中的搜索。使用SimpleAdapter进行列表视图。

ListView  listView;
SimpleAdapter adapter;
private static ArrayList<HashMap<String, Object>> docs;
private static ArrayList<HashMap<String, Object>> searchResults;

EditText editText;

    public void onCreate(Bundle savedInstanceState) {

    listView = (ListView) findViewById(R.id.list);
    editText = (EditText) findViewById(R.id.text);

    docs = new ArrayList<HashMap<String, Object>>();
    // values for docs
    getUrlDocs(false);


    searchResults = new ArrayList<HashMap<String,Object>>();

    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

          int textlength = editText.getText().length();
          String searchString= editText.getText().toString();
          searchResults.clear();
          String attr = null;
            for (int i = 0; i < docs.size(); i++)
            {

              attr = docs.get(i).get(FIRST).toString().trim();
              Log.i("attr", attr+"");

                if (textlength  <= attr.length())
                {

                    if (searchString.equalsIgnoreCase(attr.substring(0,textlength)))
                    {
                      searchResults.add(docs.get(i));
                    }
                }
            }

            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);

        }

        @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    

        }
    });
     

}