jeudi 28 février 2019

Update value of a Recyclerview with Webservice response

I have a complex problem here.

I initialize my RecyclerView with some datas but asynchronously I'm getting data from my Webservice and I want to use those data in order to have them in my RecyclerView but it's "too late" because my RecyclerView is already initialized.

Here is some code :

First of all I'm initializing my Recyclerview with some datas

JSONArray medias; <- Global variable
ServiceProvider serviceProvider = new ServiceProvider(); <- Global variable where I'm doing my request

for (int j = 0; j < 3; j += 1) {
     JSONObject newElem = new JSONObject();
     newElem.put("path", "test");
     newElem.put("state", "ongoing");
     newElem.put("name", "element " + j);
     medias.put(newElem);
     serviceProvider.addMedia(getActivity(), new CallbackInterface() {
      @Override
      public void callbackaddMedia(Boolean success, JSONObject obj) {
         if (success && obj != null)
         {
            //Here I want to use the response object like
            //Update medias item state by obj.optString("state");
         }
      }
   });
   initRecyclerView();
}

private void initRecyclerView(){
    adapter = new DocumentsAdapter(getActivity(),false, medias, this);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());

    binding.recyclerAll.setLayoutManager(mLayoutManager);
    binding.recyclerAll.setItemAnimator(new DefaultItemAnimator());
    binding.recyclerAll.setAdapter(adapter);
}

And like I said , Asynchronously I'm calling a Webservice which send me this response :

{
    "success": true,
    "mediaId": "307",
    "state": "success"
}




Aucun commentaire:

Enregistrer un commentaire