SHARE
TWEET

Activity

Neyasbit Mar 26th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.recyclerviewtest;
  2.  
  3. import androidx.annotation.NonNull;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import androidx.recyclerview.widget.LinearLayoutManager;
  6. import androidx.recyclerview.widget.RecyclerView;
  7. import retrofit2.Call;
  8. import retrofit2.Callback;
  9. import retrofit2.Response;
  10.  
  11. import android.os.Bundle;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.TextView;
  16.  
  17. import com.example.recyclerviewtest.models.Feed;
  18. import com.example.recyclerviewtest.networks.ApiService;
  19. import com.example.recyclerviewtest.networks.RetrofitBuilder;
  20.  
  21. import java.util.ArrayList;
  22. import java.util.List;
  23.  
  24. public class StartActivity extends AppCompatActivity {
  25.     ApiService service;
  26.     private RecyclerView itemList;
  27.     private LinearLayoutManager horizontalLinearLayoutManager;
  28.     private ItemAdapter adapter;
  29.     private List<Feed.FeedData> items = new ArrayList<>();
  30.     private Feed data;
  31.  
  32.     @Override
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_start);
  36.  
  37.         service = RetrofitBuilder.createService(ApiService.class);
  38.         itemList = findViewById(R.id.recycler);
  39.         LinearLayoutManager manager = new LinearLayoutManager(this);
  40.         itemList.setLayoutManager(manager);
  41.         adapter = new ItemAdapter(items);
  42.         itemList.setAdapter(adapter);
  43.         loadItems();
  44.  
  45.  
  46.     }
  47.  
  48.     private void loadItems() {
  49.         service.getFeed().enqueue(new Callback<Feed>() {
  50.             @Override
  51.             public void onResponse(Call<Feed> call, Response<Feed> response) {
  52.                 if (response.isSuccessful()) {
  53.                     data = response.body();
  54.                     items.addAll(data.getData());
  55.                     adapter.setItems(items);
  56.  
  57.                 }
  58.             }
  59.  
  60.             @Override
  61.             public void onFailure(Call<Feed> call, Throwable t) {
  62.  
  63.             }
  64.         });
  65.     }
  66.  
  67.     private class ItemAdapter extends RecyclerView.Adapter<ItemViewHolder>{
  68.         private List<Feed.FeedData> items;
  69.  
  70.         public ItemAdapter(List<Feed.FeedData> items) {
  71.             this.items = items;
  72.         }
  73.  
  74.         public void setItems(List<Feed.FeedData> items) {
  75.             this.items = items;
  76.             notifyDataSetChanged();
  77.         }
  78.  
  79.         @NonNull
  80.         @Override
  81.         public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  82.             View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_card, parent, false);
  83.             return new ItemViewHolder(view);
  84.         }
  85.  
  86.         @Override
  87.         public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
  88.             Feed.FeedData item = items.get(position);
  89.             holder.bind(item);
  90.         }
  91.  
  92.         @Override
  93.         public int getItemCount() {
  94.             return items.size();
  95.         }
  96.     }
  97.  
  98.     private class ItemViewHolder extends RecyclerView.ViewHolder {
  99.         TextView tv_name;
  100.         TextView tv_location;
  101.         TextView tv_text;
  102.         public ItemViewHolder(@NonNull View itemView) {
  103.             super(itemView);
  104.             tv_location = findViewById(R.id.tv_location);
  105.             tv_name = findViewById(R.id.tv_name);
  106.             tv_text = findViewById(R.id.tv_text);
  107.         }
  108.         void bind (Feed.FeedData type) {
  109.             tv_location.setText(type.getCity().getCityData().getName());
  110.             tv_name.setText(type.getUser().getUserData().getName());
  111.             tv_text.setText(type.getText());
  112.         }
  113.     }
  114. }
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