0

How to get the ArrayList from JSONObject request?

I tried interface but it doesn't work. Setter and Getter doesn't work too. I am using a RecyclerView.

This is my code,

public class EditDoctor extends Fragment {
 RecyclerView recyclerView;
 String key;
 String url;
 RecyclerAdapterEditDoctor edit;
 SharedPreferences sharedPreferences;
 RequestQueue queue;
 ArrayList<EditDoctorModel> listOfPojo = new ArrayList<>();
 public EditDoctor()
 {
 }
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaneState)
 {
 View view = inflater.inflate(R.layout.editdoctor,container,false);
 Context context = getActivity();
 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
 key = sharedPreferences.getString("key", "");
 return view;
 }
 @Override
 public void onViewCreated(View view,Bundle savedInstanceState) {
 setRetainInstance(true);
 }
 @Override
 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
 super.onActivityCreated(savedInstanceState);
 recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler1);
 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
 linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
 recyclerView.setLayoutManager(linearLayoutManager);
 queue = Volley.newRequestQueue(getActivity());
 sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
 key = sharedPreferences.getString("key", "");
 url = "http://192.168.0.153:9091/drrate/api/profile/Search?page=1&size=10&filter=[{\"col\":\"MemberKey\",\"val\":\""+key+"\",\"cond\":\"Equal\"}]";
 getJSONRequest();
 edit = new RecyclerAdapterEditDoctor(getActivity(),listOfPojo);
 recyclerView.setAdapter(edit);
 }
 // This is the Request 
 public void getJSONRequest() {
 JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,
 new Response.Listener<JSONObject>() {
 @Override
 public void onResponse(JSONObject response) {
 ArrayList<EditDoctorModel>temp = new ArrayList<>();
 try {
 JSONArray Jarray = response.getJSONArray("Data");
 for (int i = 0; i < Jarray.length(); i++) {
 JSONObject object = Jarray.getJSONObject(i);
 EditDoctorModel editDoctorModel = new EditDoctorModel();
 editDoctorModel.setFullName(object.getString("Name"));
 editDoctorModel.setProfession(object.getString("Profession"));
 editDoctorModel.setUrl(object.getString("Image"));
 temp.add(editDoctorModel);
 }
 // this work but android monitor says that skipping layout adapter not attached
 // edit = new RecyclerAdapterEditDoctor(getActivity(),listOfPojo);
 // recyclerView.setAdapter(edit);
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 }
 });
 queue.add(req);
}

Please help me. Thanks in advance.

Neerajlal K
6,8263 gold badges26 silver badges34 bronze badges
asked Sep 2, 2016 at 8:41
5
  • 1
    Please read stackoverflow.com/help/how-to-ask. Show us what you already tried and what is not working. Commented Sep 2, 2016 at 8:42
  • I just need to get the the array list which is the temp Commented Sep 2, 2016 at 8:54
  • Show your JSON too. Commented Sep 2, 2016 at 8:59
  • Show your JSON then we can help Commented Sep 2, 2016 at 9:04
  • guys you mean json model? public class EditDoctorModel { String fullName; String profession; String url; public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public String getProfession() { return profession; } public void setProfession(String profession) { this.profession = profession; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } } Commented Sep 2, 2016 at 9:25

2 Answers 2

2

Making some chages in your code

public class EditDoctor extends Fragment {
 RecyclerView recyclerView;
 String key;
 String url;
 RecyclerAdapterEditDoctor edit;
 SharedPreferences sharedPreferences;
 RequestQueue queue;
 ArrayList<EditDoctorModel> listOfPojo;
 public EditDoctor()
 {
 }
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaneState)
 {
 View view = inflater.inflate(R.layout.editdoctor,container,false);
 Context context = getActivity();
 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
 key = sharedPreferences.getString("key", "");
 return view;
 }
 @Override
 public void onViewCreated(View view,Bundle savedInstanceState) {
 setRetainInstance(true);
 }
 @Override
 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
 super.onActivityCreated(savedInstanceState);
 recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler1);
 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
 linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
 recyclerView.setLayoutManager(linearLayoutManager);
 queue = Volley.newRequestQueue(getActivity());
 sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
 key = sharedPreferences.getString("key", "");
 url = "http://192.168.0.153:9091/drrate/api/profile/Search?page=1&size=10&filter=[{\"col\":\"MemberKey\",\"val\":\""+key+"\",\"cond\":\"Equal\"}]";
 getJSONRequest();
 edit = new RecyclerAdapterEditDoctor(getActivity(),listOfPojo);
 recyclerView.setAdapter(edit);
 }
 // This is the Request 
 public void getJSONRequest() {
 JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,
 new Response.Listener<JSONObject>() {
 @Override
 public void onResponse(JSONObject response) {
 listOfPojo = new ArrayList<>();
 try {
 JSONArray Jarray = response.getJSONArray("Data");
 for (int i = 0; i < Jarray.length(); i++) {
 JSONObject object = Jarray.getJSONObject(i);
 EditDoctorModel editDoctorModel = new EditDoctorModel();
 editDoctorModel.setFullName(object.getString("Name"));
 editDoctorModel.setProfession(object.getString("Profession"));
 editDoctorModel.setUrl(object.getString("Image"));
 listOfPojo.add(editDoctorModel);
 }
 // this work but android monitor says that skipping layout adapter not attached
 // edit = new RecyclerAdapterEditDoctor(getActivity(),listOfPojo);
 // recyclerView.setAdapter(edit);
 edit.updateRecyler(listOfPojo);
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 }
 });
 queue.add(req);
}

//Add below method in your adapter

public void updateRecyler(ArrayList<EditDoctorModel> list){
this.your_adapterModel_list=list;
notifyDatasetChanged();
}

Whenever Volley gets response your recyler autmatically update.

answered Sep 2, 2016 at 9:26
Sign up to request clarification or add additional context in comments.

Comments

1

Add the data you have parsed to the dataset listOfPojo, and call notifyDataSetChanged on the adapter.

Try this,

@Override
public void onResponse(JSONObject response) {
 ArrayList<EditDoctorModel> temp = new ArrayList<>();
 try {
 JSONArray Jarray = response.getJSONArray("Data");
 for (int i = 0; i < Jarray.length(); i++) {
 JSONObject object = Jarray.getJSONObject(i);
 EditDoctorModel editDoctorModel = new EditDoctorModel();
 editDoctorModel.setFullName(object.getString("Name"));
 editDoctorModel.setProfession(object.getString("Profession"));
 editDoctorModel.setUrl(object.getString("Image"));
 temp.add(editDoctorModel);
 }
 // add the data in temp to listOfPojo
 listOfPojo.clear();
 listOfPojo.addAll(temp);
 // notify the adapter that the data has changed
 edit.notifyDataSetChanged();
 } catch (JSONException e) {
 e.printStackTrace();
 }
}
answered Sep 2, 2016 at 9:13

3 Comments

this doesnt work my problem is that when i add listofPojo.add(temp); then add this listOfPojo.addAll(temp); also this edit.notifyDataSetChanged(); in recycler none is showing the problem is that data get lost when thread of jsonobjectrequest is done
@sydney69 Its addAll and not add.
Happy coding. Good luck :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.