Advertisement
joris

Channel Fragment

Jun 23rd, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. package page;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.support.v7.widget.LinearLayoutManager;
  7. import android.support.v7.widget.RecyclerView;
  8. import android.util.Log;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.Toast;
  13.  
  14. import com.transvision.bertho.R;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.List;
  18.  
  19. import adapter.ChannelAdapter;
  20. import model.Channel;
  21. import model.ChannelResponse;
  22. import rest.ApiClient;
  23. import rest.ApiInterface;
  24. import retrofit2.Call;
  25. import retrofit2.Callback;
  26. import retrofit2.Response;
  27.  
  28.  
  29. public class HomeFragment extends Fragment {
  30.  
  31.     private RecyclerView recyclerView;
  32.     private static final String TAG = HomeFragment.class.getSimpleName();
  33.  
  34.     private ChannelAdapter adapter;
  35.     List<Channel> listChannel;
  36.  
  37.     @Override
  38.     public void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.     }
  41.  
  42.     @Override
  43.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  44.  
  45.         View rootView = inflater.inflate(R.layout.fragment_home, container, false);
  46.  
  47.         adapter = new ChannelAdapter (listChannel, R.layout.list_channel, getActivity());
  48.  
  49.         recyclerView = (RecyclerView) rootView.findViewById(R.id.movies_recycler_view);
  50.         recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  51.         recyclerView.setAdapter(adapter);
  52.  
  53.         getChannelData();
  54.  
  55.         return rootView;
  56.     }
  57.  
  58.     @Override
  59.     public void onAttach(Activity activity) {
  60.         super.onAttach(activity);
  61.     }
  62.  
  63.     @Override
  64.     public void onDetach() {
  65.         super.onDetach();
  66.     }
  67.  
  68.     public void getChannelData() {
  69.         ApiInterface apiService = ApiClient.getChannel().create(ApiInterface.class);
  70.         Call<ChannelResponse> call = apiService.getItems();
  71.  
  72.         call.enqueue(new Callback<ChannelResponse>() {
  73.             @Override
  74.             public void onResponse(Call<ChannelResponse> call, Response<ChannelResponse> response) {
  75.                 int statusCode = response.code();
  76.                 List<Channel> channel = response.body().getItems();
  77. //                recyclerView.setAdapter(new ChannelAdapter(channel, R.layout.list_channel, getActivity()));
  78.                 adapter.notifyDataSetChanged();
  79.                 showToast("CONNECTION SUCCESS");
  80.             }
  81.  
  82.             @Override
  83.             public void onFailure(Call<ChannelResponse> call, Throwable t) {
  84.                 // Log error here since request failed
  85.                 Log.e(TAG, t.toString());
  86.                 showToast("CONNECTION ERROR");
  87.             }
  88.         });
  89.     }
  90.  
  91.     public void showToast(String output){
  92.         Toast.makeText(getActivity(), output, Toast.LENGTH_SHORT).show();
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement