In my app I have implemented a recyclerview with cardview to show the list of the employee inside a company. Now I want to implement a serch option by which user can search with
IMO you missed setNotifyDatachaged() in onPostExecute() try above code
public class MyColleaguesPage extends AppCompatActivity{
private RecyclerView recyclerView;
private MyColleaguesAdapter adapter;
private List<MyColleageModel> colleagueObject;
InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
// Showing and Enabling clicks on the Home/Up button
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MyColleaguesPage.this);
recyclerView.setLayoutManager(layoutManager);
adapter = new MyColleaguesAdapter(colleagueObject, getApplicationContext());
recyclerView.setAdapter(adapter);
new JSONTask().execute();
}
public class JSONTask extends AsyncTask<String, String, List<MyColleageModel>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<MyColleageModel> doInBackground(String... params) {
colleagueObject = new ArrayList<MyColleageModel>();
try {
//withou Gson
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("contacts");
// ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
// HashMap<String, String> m_li;
MyColleageModel my_clg;
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
//my_clg=new MyColleageModel();
String val1 = jo_inside.getString("mobile");
String val2 = jo_inside.getString("mail");
String val3 = jo_inside.getString("name");
String val4 = jo_inside.optString("company");
String val5 = jo_inside.optString("department");
String val6 = jo_inside.optString("title");
my_clg = new MyColleageModel(val1, val2, val3, val4, val5, val6);
colleagueObject.add(my_clg);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(final List<MyColleageModel> result) {
super.onPostExecute(result);
adapter.notifyDataSetChanged();
}
}
private String loadJSONFromAsset() {
...........
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.recyclerviewmenu, menu);
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
search(searchView);
return true;
}
private void search(SearchView searchView) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
follow this stpes
step 1 implements SearchView.OnQueryTextListener
in youractivity
step 2 in onCreateOptionsMenu
write below code
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_album_search, menu);
final MenuItem item = menu.findItem(R.id.menu_search_album);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
return true;
}
step 3 write this in onQueryTextChange()
method
@Override
public boolean onQueryTextChange(String newText) {
List<MyColleageModel> filteredModelList = filter(data_fieldses, newText);
if (filteredModelList.size() > 0) {
//myPhotosAdapter.setFilter(filteredModelList);
return true;
} else {
Toast.makeText(MyPhotosActivity.this, "Not Found", Toast.LENGTH_SHORT).show();
return false;
}
}
step 4
private List<MyColleageModel> filter(List<MyColleageModel> models, String query) {
query = query.toLowerCase();
final List<MyColleageModel> filteredModelList = new ArrayList<>();
for (MyColleageModel model : models) {
final String text = model.getImagename().toLowerCase().toString();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
// arraylist in your adapter
myPhotosAdapter = new MyPhotosAdapter( MyPhotosActivity.this, (ArrayList<MyColleageModel>) filteredModelList);
recyclerViewMyPhotos.setLayoutManager(new GridLayoutManager(MyPhotosActivity.this, 2));
recyclerViewMyPhotos.setAdapter(myPhotosAdapter);
myPhotosAdapter.notifyDataSetChanged();
return filteredModelList;
}
ask me in case of any query