2016-09-02 71 views
0

如何从JSONObject请求ArrayList如何从JSONObject请求获取ArrayList

我试过接口,但不起作用。安装员和Getter不起作用。我正在使用RecyclerView

这是我的代码,

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); 
} 

请帮助我。提前致谢。

+1

请阅读http://stackoverflow.com/help/how-to-ask。告诉我们你已经尝试了什么,什么不工作。 –

+0

我只需要获取温度的数组列表 – sydney69

+0

也显示你的'JSON'。 –

回答

1

将您已解析的数据添加到数据集listOfPojo,并在适配器上调用notifyDataSetChanged

试试这个,

@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(); 
    } 
} 
+0

这不起作用我的问题是,当我添加listofPojo.add(temp);然后添加这个listOfPojo.addAll(temp);也是这个edit.notifyDataSetChanged();在回收站中,没有人显示问题是当jsonobjectrequest的线程完成时数据丢失 – sydney69

+0

@ sydney69它的'addAll'而不是'add'。 –

+1

很好,谢谢你的工作 – sydney69

2

使一些chages在你的代码

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); 
} 

//添加下面的方法在您的适配器

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

每当凌空得到响应您的recyler autmatically更新。

+0

感谢您的想法。问题已经解决了 – sydney69

+0

@ sydney69好吧 –