Livedata Observe not running

I tried implementing ViewModel using this. But the observe is never called.

Basically this app makes network request on the SAMPLE_URL, converts JSON to List and show the list through bookView. The app is working fine without ViewModel. With ViewModel the app runs but the observe is never called and no data is shown.

What am I doing wrong here?

BookActivity class:

 public BookAdapter bookAdapter;
    ListView bookView;
    public final static String SAMPLE_URL = "";
    public ArrayList<Book> books = new ArrayList<>();
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        bookView = findViewById(;
        bookAdapter = new BookAdapter(BookActivity.this,books);
        BookViewModel bookViewModel = new ViewModelProvider(this).get(BookViewModel.class);
        bookViewModel.getBooks().observe(this, books -> {
            Log.d("INSIDE", "observe");
            bookAdapter = new BookAdapter(this,books);

BookViewModel class:

public class BookViewModel extends ViewModel {
    public MutableLiveData<List<Book>> books;
    public LiveData<List<Book>> getBooks(){
        if (books == null) {
            books = new MutableLiveData<>();
        return books;
    private void loadBooks() {
    Thread thread = new Thread(new Runnable() {
        public void run() {
            URL url = QueryUtility.createURL(BookActivity.SAMPLE_URL);
                assert url != null;
                String JSONResponse = QueryUtility.ReadFromStream(QueryUtility.MakeHTTPRequest(url));
                books = new MutableLiveData<>(QueryUtility.extractBooksFromJSON(JSONResponse));
            catch (IOException | JSONException ioException){


Your thread creates a new instance of MutableLiveData for books. The original lazily-loaded instance that you probably observed is never updated. You should update the value of the existing instance.


Leave a Reply

Your email address will not be published. Required fields are marked *