Created
June 11, 2019 10:38
-
-
Save wangerekaharun/fe77bef2aeb9e55d54adfe88ee3d22b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (c) Amwollo Victor 2019. | |
| */ | |
| package com.ovicko.viewmodelandroid; | |
| import android.arch.lifecycle.Observer; | |
| import android.arch.lifecycle.ViewModelProviders; | |
| import android.support.annotation.Nullable; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.support.v7.widget.LinearLayoutManager; | |
| import android.support.v7.widget.RecyclerView; | |
| import android.widget.Toast; | |
| import com.ovicko.viewmodelandroid.model.Hit; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class MainActivity extends AppCompatActivity { | |
| ImageAdapter imageAdapter; | |
| List<Hit> hits = new ArrayList(); | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| RecyclerView recyclerView = findViewById(R.id.recyclerview); | |
| imageAdapter = new ImageAdapter(MainActivity.this,hits); | |
| recyclerView.setAdapter(imageAdapter); | |
| recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
| recyclerView.setHasFixedSize(true); | |
| ImageViewModel imageViewModel = ViewModelProviders.of(this).get(ImageViewModel.class); | |
| imageViewModel.getPixaImages().observe(this, new Observer<List<Hit>>() { | |
| @Override | |
| public void onChanged(@Nullable List<Hit> hits) { | |
| //call your method from adapter to update it with the data | |
| imageAdapter.setImageAdapter(hits); | |
| Toast.makeText(MainActivity.this, "SIZE_"+hits.size(), Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| imageViewModel.getError().observe(this, new Observer<String>() { | |
| @Override | |
| public void onChanged(@Nullable String error) { | |
| Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment