-1

I am working with json. I have been successful in parsing json and can show the result in listview(images and texts). Now I want to write a search function to search in my json output. I have writen a function, it searches name, but all names in json start with UpperCase and that is poses as problem. I had also tested my code without UpperCase case and it was working at that moment. this is my code

public void SearchFunction(String searchWord) {

    itemList.clear();
    adapter.notifyDataSetChanged();



    if (searchWord.equals(" ")) {
        for (int i = 0; i < contents.size(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();

            map.put(KEY_name, contents.get(i).name);
            map.put(KEY_id, contents.get(i).id);
            itemList.add(map);
        }
    } else {
        for (int i = 0; i < contents.size(); i++) {
            if (contents.get(i).name.indexOf(searchWord) >= 0) {
                HashMap<String, String> map = new HashMap<String, String>();

                map.put(KEY_name, contents.get(i).name);
                map.put(KEY_id, contents.get(i).id);

                itemList.add(map);

            }

        }
    }
    adapter.notifyDataSetChanged();

}




listResult.addTextChangedListener(new TextWatcher() {
                public void afterTextChanged(Editable s) {
                }

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

                }

                public void onTextChanged(CharSequence s, int start,
                        int before, int count) {

                    String searchWord = listResult.getText().toString();
                    list.setVisibility(View.VISIBLE);
                    SearchFunction(searchWord);

                }

            });

I want to check if the solution is in UpperCase. I want my code to work pefectly with or without UpperCase. Please help me with the solution.

KaranJ
  • 48
  • 1
  • 10
user3345767
  • 349
  • 1
  • 2
  • 11

2 Answers2

1

You may use .toUpperCase() or .toLowerCase()

I mean something like

contents.get(i).name.toLowerCase().indexOf(searchWord.toLowerCase())
Artem
  • 1,566
  • 1
  • 10
  • 15
0

You may be interested by the article "Adding Custom Suggestions" on Android developers.

The method for rewriting the query is what you looking for I guess.

Pierre-Jean
  • 1,872
  • 1
  • 15
  • 21