0 Replies - 478 Views - Last Post: 11 June 2020 - 03:28 AM

#1 TheStudent555   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 06-February 19

MutableLiveData not changing UI

Posted 11 June 2020 - 03:28 AM

Making calls to remote server, and placing data in arraylist. Then I am placing the arraylist in a mutablelivedata object. The mutablelivedata object is then observed in MainFragment. However, the MainFragment is not updating/registering the changes made to MutableLiveData

MainFragment.java
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle onSaveInstanceState){

        fragment_viewmodel = ViewModelProviders.of(getActivity())
                .get(FragmentViewModel.class);

        fragment_viewmodel.initialize(getActivity().getApplication());

       fragment_viewmodel.getObject().observe(getViewLifecycleOwner(), new Observer<ArrayList<Object>>() {
            @Override
            public void onchanged(ArrayList<Object> object) {

                     // Never Called, When arraylist has size of 1

                }

            }
        });


}




FragmentViewModel.class
 private MutableLiveData<ArrayList<Object>> object;
 public void initialize(Application application){

        if(repository == null) {
            object_repository = ObjectRepository.getInstance();
        }


        object_repository = ObjectRepository.getInstance();
        object = repository.get_object_data();
    }

    public MutableLiveData<ArrayList<Object>> getObject(){
        return object;
    }




ObjectRepository
   private final MutableLiveData<ArrayList<Object>> object_data = new MutableLiveData<>();
    private ArrayList<Object> objects = new ArrayList<>();

    public MutableLiveData<ArrayList<Object>> get_object_data(){
       
        return object_data;

    }

  private void network_call(){


      
  
        Network network = new Network();
        network.Execute(new NetworkCallback() {
            @Override
            public void network_response(String json_response) {

          
                JSONArray server_response = null;
                try{

                    server_response = new JSONArray(json_response);

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

          

                objects.clear();

                for(int i = 0; i < server_response.length(); i++){

                    try {

                        String var_a = server_response.getJSONObject(i).getString("variable_a");
                        String var_b = server_response.getJSONObject(i).getString("variable_b");

                        Object obj = new Object();
                        obj.var_a = var_a;
                        obj.var_b = var_b;

                        objects.add(obj);

                    }catch(JSONException json_error){
                        json_error.printStackTrace();
                        return;
                    }


                }

            
                object_data.setValue(objects);
             
            }

            @Override
            public void network_error(VolleyError error) {
           
                error.printStackTrace();
            }
        });

    }






The network_call() is called in onPerformSync in a sync adapter class.
Not sure what would cause the mutablelivedata not to update? Does anyone know what could be the problem?

This post has been edited by TheStudent555: 11 June 2020 - 11:04 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1