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 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> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List doInBackground(String... params) {
colleagueObject = new ArrayList();
try {
//withou Gson
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("contacts");
// ArrayList> formList = new ArrayList>();
// HashMap 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 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);
}