SHARE
TWEET

MainActivity

a guest Apr 21st, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.portal_informasi;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.support.v7.widget.LinearLayoutManager;
  8. import android.support.v7.widget.RecyclerView;
  9. import android.util.Log;
  10. import android.widget.Toast;
  11.  
  12. import com.example.portal_informasi.KONEKSI.ApiServices;
  13. import com.example.portal_informasi.KONEKSI.InitRetrofit;
  14. import com.example.portal_informasi.RESPONSE.BeritaItem;
  15. import com.example.portal_informasi.RESPONSE.ResponseBerita;
  16.  
  17. import java.util.List;
  18.  
  19. import retrofit2.Call;
  20. import retrofit2.Callback;
  21. import retrofit2.Response;
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24.  
  25.     // Deklarasi Widget
  26.     private RecyclerView recyclerView;
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_main);
  32.  
  33.         recyclerView = (RecyclerView) findViewById(R.id.rvListBerita);
  34.         // RecyclerView harus pakai Layout manager
  35.         recyclerView.setLayoutManager(new LinearLayoutManager(this));
  36.         LinearLayoutManager llm = new LinearLayoutManager(this);
  37.         llm.setOrientation(LinearLayoutManager.VERTICAL);
  38.  
  39.         // Eksekusi method
  40.         tampilBerita();
  41.     }
  42.         private void tampilBerita() {
  43.             ApiServices api = InitRetrofit.getInstance();
  44.             // Siapkan request
  45.             Call<ResponseBerita> beritaCall = api.request_show_all_berita();
  46.             // Kirim request
  47.             beritaCall.enqueue(new Callback<ResponseBerita>() {
  48.                 @Override
  49.                 public void onResponse(Call<ResponseBerita> call, Response<ResponseBerita> response) {
  50.                     // Pasikan response Sukses
  51.                     if (response.isSuccessful()){
  52.                         Log.d("response api", response.body().toString());
  53.                         // tampung data response body ke variable
  54.                         List<BeritaItem> data_berita = response.body().getBerita();
  55.                         boolean status = response.body().isStatus();
  56.                         // Kalau response status nya = true
  57.                         if (status){
  58.                             // Buat Adapter untuk recycler view
  59.                             AdapterBerita adapter = new AdapterBerita(MainActivity.this, data_berita);
  60.                             recyclerView.setAdapter(adapter);
  61.                         } else {
  62.                             // kalau tidak true
  63.                             Toast.makeText(MainActivity.this, "Tidak ada berita untuk saat ini", Toast.LENGTH_SHORT).show();
  64.                         }
  65.                     }
  66.                 }
  67.                 @Override
  68.                 public void onFailure(Call<ResponseBerita> call, Throwable t) {
  69.                     // print ke log jika Error
  70.                     t.printStackTrace();
  71.                 }
  72.             });
  73.         }
  74.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top