需要为materialsearchview添加onitemclick listner

时间:2017-07-22 12:40:52

标签: java android searchview

我的materialsearchview的名称是searchView,有人请帮我为searchView添加onitemclicklistner。这里的菜单项从数据库中检索,并显示为列表。当我点击列表项时,我需要获得点击的项目

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_grids);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            sharedPreferences = getSharedPreferences(MyPref, Context.MODE_PRIVATE);
            editor = sharedPreferences.edit();
            locationStatus =sharedPreferences.getString(locationStatus,"");
            setSupportActionBar(toolbar);
            searchView = (MaterialSearchView) findViewById(R.id.search_view);
            atm = new Atm();
            searchView.setCursorDrawable(R.drawable.custom_cursor);
            searchView.setEllipsize(true);
            autoservice=(AutoCompleteTextView)findViewById(R.id.auto);

GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
           // searchView.setSuggestions(new String[]{"ATM","CANARA","cater","travel","grocery","shop"});
            autoservice.addTextChangedListener(this);

       searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                   // Snackbar.make(findViewById(R.id.container), "Query: " + query, Snackbar.LENGTH_LONG)
                          //  .show();

                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                     m=newText;
                    searchdetails();
                    //Do some magic
                    return false;
                }
            });


            searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
                @Override
                public void onSearchViewShown() {
                    //Do some magic
                }

                @Override
                public void onSearchViewClosed() {
                    //Do some magic
                }
            });
            CustomGrid adapter = new CustomGrid(Grids.this, web, imageId);
            grid = (GridView) findViewById(R.id.grid);
            grid.setAdapter(adapter);
            //lv = (ListView) findViewById(R.id.list);
            final ArrayList<String> todoItem = new ArrayList<String>();

            aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,todoItem);
            //lv.setAdapter(aa);
        }

        private void searchdetails() {
            StringRequest stringRequest=new StringRequest(Request.Method.POST,JSON_URL,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            Log.i("response","response is"+response);
                            try {
                                JSONArray jsonArray = new JSONArray(response);
                                JSONObject json = null;
                                // String stringArray[];
                                // stringArray=new String[jsonArray.length()];
                                // ArrayList<String>ar=new ArrayList<String>();
                                String xcoords[] = new String[jsonArray.length()];
                                // String x[]=new String[jsonArray.length()];
                                if (jsonArray.length() != 0) {
                                    for (i = 0; i < jsonArray.length(); i++) {
                                        json = jsonArray.getJSONObject(i);
                                        xcoords[i] = json.getString("name");
                                        //names.add(json.getString("names"));
                                        // x[i]=json.getString("location_name");
                                        //  ar.add(x[i]);
                                        //  ar.add(xcoords[i]);

                                    }
                                    searchView.setSuggestions(xcoords);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                           // Toast.makeText(Grids.this,error.toString(),Toast.LENGTH_LONG).show();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("keyword",m);

                    return params;
                }
            };

            RequestQueue requestQueue= Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_friend_list_activity, menu);
            MenuItem item = menu.findItem(R.id.action_search);

            searchView.setMenuItem(item);

            return true;
        }
                    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) {
                ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                if (matches != null && matches.size() > 0) {
                    String searchWrd = matches.get(0);
                    if (!TextUtils.isEmpty(searchWrd)) {
                        searchView.setQuery(searchWrd, false);
                    }
                }

                return;
            }
            super.onActivityResult(requestCode, resultCode, data);
        }

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

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            prepareMyList();
        }

        private void prepareMyList() {
            StringRequest stringRequest = new StringRequest(Request.Method.POST, SERVICE, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.i("response", "response is" + response);
                    try {
                        //JSONObject jObj = new JSONObject(response);
                        ArrayList<String> arrlist = new ArrayList<String>();
                        JSONArray jsonarray = new JSONArray(response);
                        JSONObject json = null;
                        for (int i = 0; i < jsonarray.length(); i++) {
                            json = jsonarray.getJSONObject(i);
                            final String strValue = json.getString("location_name");

                            //  String strValue=jsonarray.getString(i);
                            arrlist.add(strValue);

                            //   Toast.makeText(getBaseContext(),"services:" + jsonarray.getJSONArray(i), Toast.LENGTH_SHORT).show();
                          //  Toast.makeText(getBaseContext(), "services:" + json.getString("location_name"), Toast.LENGTH_SHORT).show();
                            adapter1 = new ArrayAdapter<String>(
                                    getBaseContext(),
                                    android.R.layout.simple_dropdown_item_1line,
                                    arrlist);

                            autoservice.setAdapter(adapter1);
                        }
                    } catch (JSONException e) {
                        // JSON error
                        e.printStackTrace();
                     //   Toast.makeText(getBaseContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("keyword", autoservice.getText().toString());
                    return params;
                }

            };
            RequestQueue requestQueue = Volley.newRequestQueue(getBaseContext());
            requestQueue.add(stringRequest);
        }
        @Override
        public void afterTextChanged(Editable s) {

        }
    }

0 个答案:

没有答案