lundi 21 décembre 2020

Android MVVM design pattern not showing empty values in logcat but when calling api to set data it is showing size of arraylist in repository class

IN THIS CLASS WHEN I AM CALLING API I AM GETTING SIZE OF THE LIST IN LOGCAT UNDER getFromApi() METHOD BUT EMPTY SIZE IN getIMAGES() METHOD I AM NEW ON MVVM DESIGN PATTERN CAN SOMEONE HELP ME OUT THANKS

public class ImagesRepositoy {
private static ImagesRepositoy instance;
private ArrayList<Images> dataset = new ArrayList<>();
private RequestQueue queue;
private static Context mCtx;
private StringRequest request;

public ImagesRepositoy() {
}

public ImagesRepositoy(Context context) {
    mCtx = context;
}

public static ImagesRepositoy getInstance() {
    if (instance == null) {
        instance = new ImagesRepositoy();
    }
    return instance;
}

public MutableLiveData<List<Images>> getIMAGES() {
    getFromApi();
    MutableLiveData<List<Images>> data = new MutableLiveData<>();
    data.setValue(dataset);// here I am getting empty array size
    Log.e("DATA",data.getValue().toString());
    return data;
}

private void getFromApi() {
    Log.e("YUPP", "HANN");
    request = new StringRequest(Request.Method.GET, ImageApi + "APIKEY&limit=25", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("responsefromserv", response + "null");
            try {


                JSONObject jsonObject = new JSONObject(response);
                JSONObject images, original, jsonObject3;
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                Log.d("DATAATA", jsonObject.getJSONArray("data") + ".");


                for (int i = 0; i < jsonArray.length(); i++) {
                    Images Imgs = new Images();
                    images = jsonArray.getJSONObject(i);
                    original = images.getJSONObject("images");
                    jsonObject3 = original.getJSONObject("original");
                    Imgs.setUrl(jsonObject3.getString("url"));
                    Log.e("JSONE3", Imgs.getUrl() + "");
                    dataset.add(Imgs);   // HERE I AM GETTING 25 size

                }


            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("error", error.toString());
        }
    });
    queue = Volley.newRequestQueue(mCtx);

    queue.add(request);

}}

VIEW MODEL CLASS ALSO SHOWING EMPTY ARRAY HERE ALSO

  public class HomeViewModel extends ViewModel {
private ImagesRepositoy repositoy;
private MutableLiveData<List<Images>> url;


public void init() {
    if (url != null) {
        return;
    }
    Log.e("INIT","OK");
    repositoy = ImagesRepositoy.getInstance();
    url = repositoy.getIMAGES();
}

public LiveData<List<Images>> getIMAGES() {
    Log.e("UUUU",url.getValue().toString());
    return url;
}}

THIS IS HOME FRAGMENT CLASS WHERE I AM FILLING RECYCLERVIEW AND WHEN I LOG THE VALUE IT ALSO SHOWING ME THE EMPTY ARRAY I DON'T KNOW WHY I HAVE TRIED MANY SOLUTION ON S.O. NONE OF THEM WORKED

 public class HomeFragment extends Fragment {

private List<Images> imagesList = new ArrayList<>();
private RecyclerView recyclerView;
private RequestQueue queue;
private MyGifAdapter myGifAdapter;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_home, container, false);
    recyclerView = root.findViewById(R.id.my_recycler_view);
    HomeViewModel homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
    new ImagesRepositoy(getActivity());
    homeViewModel.init();
    homeViewModel.getIMAGES().observe(getActivity(), new Observer<List<Images>>() {
        @Override
        public void onChanged(List<Images> s) {
            myGifAdapter.notifyDataSetChanged();
        }
    });
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
    recyclerView.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
    Log.e("VMMM",homeViewModel.getIMAGES().getValue()+".");     //THIS IS GIVING EMPTY ARRAYLIST
    myGifAdapter = new MyGifAdapter(getContext(), homeViewModel.getIMAGES().getValue());
    recyclerView.setAdapter(myGifAdapter); // set the Adapter to RecyclerView
    //fetchGifs();

    return root;
}

Aucun commentaire:

Enregistrer un commentaire