Hello, I am trying to implement a generic fragment that contains a SearchView with autocomplete data coming from a external api. I am having trouble populating the list.
Appreciate any pointers. Here is my menu: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:icon="@drawable/search" android:id="@+id/action_search" android:title="@string/title_search" app:showAsAction="ifRoom|withText" app:actionViewClass="android.support.v7.widget.SearchView" /> </menu> <android.support.v7.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="#2196F3" android:clickable="true" android:minHeight="?actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <TextView android:id="@+id/toolbar_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="@string/home" /> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> </android.support.v7.widget.Toolbar> Fragment snippet: @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // Inflate the menu; this adds items to the action bar if it is present. SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE); try { getActivity().getMenuInflater().inflate(R.menu.drawer_view, menu); mRefresh = menu.findItem(R.id.action_search); //mListView.setTextFilterEnabled(true); if (mRefresh != null) { searchView = (SearchView) mRefresh.getActionView(); } if (searchView != null) { searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName())); searchView.setIconified(false); searchView.setSuggestionsAdapter(adapter); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { // Toast like print Log.d("SearchViewSubmit", "In searchView listener" + query); if (!searchView.isIconified()) { searchView.setIconified(true); } mRefresh.collapseActionView(); return false; } @Override public boolean onQueryTextChange(String s) { // Start volley search only of the first 3 characters are stored if (s.length() >= 3) { updateList(s); return true; } mListView.clearTextFilter(); return false; } }); } //return true; }catch (Exception ex){ ex.printStackTrace(); } } // Volley request public void updateList(String place) { String input = ""; try { input = URLEncoder.encode(place, "utf-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } String output = "json"; url = AppConfig.HOST + AppConfig.GET_ITEMS + input; JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { JSONArray ja = response.getJSONArray(TAG_RESULT); String[] columnNames = {"item_name"}; // Filter data // Trying out options. Trying to populate matrix cursor mc = new MatrixCursor(columnNames); String[] from = {"item_name"}; int[] to = {android.R.id.text1}; String[] tempStr = new String[1]; for (int i = 0; i < ja.length(); i++) { JSONObject c = ja.getJSONObject(i); String description = c.getString("name"); String id = c.getString("id"); Log.d("ItemName", description); Log.d("ItemID", id); tempStr[0]=description; //tempStr[1]=description; mc.addRow(tempStr); } // Not sure if this approach is better mListView.setAdapter(mAdapter = new ArrayAdapter<String>(cTxt, android.R.layout.simple_list_item_1, tempStr)); mListView.setTextFilterEnabled(true); mListView.setFilterText(description.toString()); // Or this //adapter = new SimpleCursorAdapter(cTxt,android.R.layout.simple_list_item_1,mc,from,to,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); //searchView.setSuggestionsAdapter(adapter); } catch (Exception e) { } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); // Submit to queue HttpRequestQueue.getInstance(cTxt).addToRequestQueue(jsonObjReq); } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/a120eed3-42ff-4888-84cc-f317838d2f0e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.