Guest User

Untitled

a guest
Nov 13th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. package com.example.guesswhothesmurfs.adapters;
  2.  
  3. import android.app.AlertDialog;
  4. import android.app.Dialog;
  5. import android.app.LauncherActivity;
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.net.Uri;
  10. import android.os.Build;
  11. import android.support.annotation.RequiresApi;
  12. import android.support.v7.widget.RecyclerView;
  13. import android.util.Log;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.view.WindowManager;
  18. import android.widget.Button;
  19. import android.widget.EditText;
  20. import android.widget.ImageView;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23.  
  24. import com.example.guesswhothesmurfs.R;
  25. import com.example.guesswhothesmurfs.adapters.ViewHolders.GuessWhoTheSmurfsViewHolder;
  26. import com.example.guesswhothesmurfs.models.GuessWhoTheSmurfsCharacter;
  27. import com.example.guesswhothesmurfs.persistency.CharacterContract;
  28.  
  29. import java.util.ArrayList;
  30. import java.util.Collections;
  31. import java.util.List;
  32. import java.util.zip.Inflater;
  33.  
  34. public class GuessWhoTheSmurfsAdapter extends RecyclerView.Adapter<GuessWhoTheSmurfsViewHolder> {
  35.  
  36.     /**
  37.      * Data class containing the characters which are shown in the list
  38.      */
  39.     List<GuessWhoTheSmurfsCharacter> list = Collections.emptyList();
  40.  
  41.  
  42.     public GuessWhoTheSmurfsAdapter(List<GuessWhoTheSmurfsCharacter> list, Context context) {
  43.         this.list = list;
  44.         this.context = context;
  45.     }
  46.  
  47.  
  48.     /**
  49.      * Reference to our activity
  50.      */
  51.     private Context context;
  52.  
  53.     private ViewGroup parent;
  54.  
  55.     @Override
  56.     public GuessWhoTheSmurfsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  57.         this.parent = parent;
  58.         //Inflate the layout, initialize the View Holder
  59.         View view = LayoutInflater.from(this.parent.getContext()).inflate(R.layout.row_layout, parent, false);
  60.         GuessWhoTheSmurfsViewHolder holder = new GuessWhoTheSmurfsViewHolder(view);
  61.         Log.d(this.getClass().getSimpleName(), "Creating viewholder");
  62.         return holder;
  63.  
  64.     }
  65.  
  66.     @Override
  67.     public void onBindViewHolder(GuessWhoTheSmurfsViewHolder holder, int position) {
  68.         Log.d(this.getClass().getSimpleName(), "Binding position " + position);
  69.         holder.setData(list.get(position));
  70.     }
  71.  
  72.     @Override
  73.     public int getItemCount() {
  74.         //returns the number of elements the RecyclerView will display
  75.         return list.size();
  76.     }
  77.  
  78.     public List<GuessWhoTheSmurfsCharacter> getAllCharacters(){
  79.         return list;
  80.     }
  81.  
  82.     /**
  83.      *  Remove a RecyclerView item containing a specified Data object
  84.      * @param position The pĂ´stition of the data to remove
  85.      */
  86.     public GuessWhoTheSmurfsCharacter view(int position) {
  87.         final GuessWhoTheSmurfsCharacter character = list.get(position);
  88.         return character;
  89.     }
  90.  
  91.     public void remove(int position) {
  92.         list.remove(position);
  93.         notifyItemRemoved(position);
  94.         notifyDataSetChanged();
  95.     }
  96.  
  97.     public void update(int position, String name, String description){
  98.         GuessWhoTheSmurfsCharacter character = view(position);
  99.         character.setName(name);
  100.         character.setDescription(description);
  101.         list.set(position, character);
  102.         notifyDataSetChanged();
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment