0
votes

// i am getting data from db in allShops
JsonArrayRequest jsonArrayRequest; jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener() { @Override public void onResponse(JSONArray response) {

            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject jsonObject = response.getJSONObject(i);
                    //allShops is an arraylist
                    allShops.add(jsonObject.getString("name"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            //user enters text in edittext et_search
            et_search.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after)
                {


                }

                @Override
                public void onTextChanged(CharSequence cs, int start, int before, int count)
                {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    //here i am adding arraylist to adapter but want it to happen after filtering the arraylist first based on  users input
                    adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,allShops);
                    (Search.this).adapter.getFilter().filter(s);
                    lv_search.setAdapter(adapter);
                }
            });
1
Is it possible yet? What have you tried and where are you getting stuck? Please add more detail to your post. .cyroxis
use java streams to filter it. These mkyong.com/java8/java-8-streams-filter-examples examples will help for uPushpikaWan
thank you @LuceferAli Raza

1 Answers

0
votes

I updated the fallowing part of code:

@Override
                public void onTextChanged(CharSequence cs, int start, int before, int count)
                {
                    if (cs.length()== 0)
                    {
                        adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,new ArrayList<String>());
                        //(Search.this).adapter.getFilter().filter(cs);
                        lv_search.setAdapter(adapter);
                    }
                    else
                    {
                        List<String> filtered = new ArrayList<String>();
                        for(String x:allShops)
                        {
                            if (x.toLowerCase().contains(cs.toString().toLowerCase()))
                            {
                                filtered.add(x);
                            }
                        }
                        adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,filtered);
                        lv_search.setAdapter(adapter);
                    }
                }