Here's the situation, I have 2 classes, a class for my fragment and a singleton to get my objects. Every time this fragment is created, it will ask my singleton to create objects using JSON from my server using Volley. However, this resulting in my singleton returning the objects before it is completely created. This means my fragment will display nothing, except in some cases that the HTTP request finish first. (I tried logging, and indeed the method calls are not in order)
What is a good approach so I can update my view after all HTTP requests are completed?
public class myFragment extends Fragment{
onCreate(){
...
UpdateUI();
}
private void updateUI(){
Factory factory = factory.get(getActivity());
things = pabrik.getThings();
...
}
}
public class Factory{
Factory factory;
List<Things> things;
private Factory(Context c){
things = new ArrayList<>();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, "10.0.2.2/...", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray arr = response.getJSONArray("user");
for (int i = 0; i < arr.length(); i++){
mBarang.add(new Barang(arr.getJSONObject(i).getString("email"), i));
Log.d("GET", arr.getJSONObject(i).getString("email"));
}
}catch (JSONException e){
e.printStackTrace();
}
}
},
...
);
mRequestQ.add(request);
}
public static synchronized Factory get(Context c){
if(this.factory == null){
this.factory = new Factory(c);
}
return this.factory;
}
public List<Things> getThings(){
return this.Things;
}
}
Aucun commentaire:
Enregistrer un commentaire