Issue
I am getting data from server as json. Everything works but notifyDataSetChanged
is not working.
BackGroundTask backGroundtask = new BackGroundtask();
backGroundTask.execute(arrayList);
private class BackGroundWorkeroneSignup extends AsyncTask<List<CheckingDTO>,CheckingDTO, CheckingDTO> {
@Override
public void onResponse(JSONObject response) {
CheckingDTO check = new CheckingDTO(email_c+"=="+pass_c,email_c,pass_c,email_c,pass_c);
arrayList.add(checkingDTO);
adapterActivity = new HistoryAdapter(arrayList);
recyclerView.setAdapter(adapterActivity);
@Override
protected void onPostExecute(CheckingDTO data) {
super.onPostExecute(data);
arrayList.clear();
List<CheckingDTO> list = new ArrayList<>();
list.add(data);
adapterActivity = new HistoryAdapter(list);
adapterActivity.notifyDataSetChanged();
}
Solution
I'm posting the relevant code that I've sent you in the email.
oncreate method
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
adapterActivity = new HistoryAdapter(arrayList);
recyclerView.setAdapter(adapterActivity);
backGroundWorkeroneSignup = new BackGroundWorkeroneSignup();
backGroundWorkeroneSignup.execute(arrayList);
background method:
try {
JSONArray jsonArray = response.getJSONArray("get_request");// this is your array containing data
for (int i=0;i<jsonArray.length();i++) {
JSONObject jobj = jsonArray.getJSONObject(i);
String id = jobj.getString("id");
String email_c =jobj.getString("pick_lat");
String pass_c = jobj.getString("pick_lon");
checkingDTO = new CheckingDTO(email_c+"=="+pass_c,email_c,pass_c,email_c,pass_c);
CheckingDTO check = new CheckingDTO(email_c+"=="+pass_c,email_c,pass_c,email_c,pass_c);
arrayList.add(check);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
on PostExecute
adapterActivity.notifyDataSetChanged();
In First Part Notice the recyclerView.setAdapter(adapterActivity);
In Background Mode:
- when you loop in Array always remember: index starts from 0 therefore it's always less then size of array i.e.
for (int i=0;i<jsonArray.length();i++)
- You should always create new object of Model class in loop for and then add it in loop. There in no need of calling the notifydatasetchanged in loop.
In onPostExecute: we call the notifydatasetchanged only because we've already added all the elements in the list.
hope that explains all of your queries. let me know if you have confusion in any case
Answered By - Sahil Manchanda
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.