Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. public class RecipeListAdapter extends RecyclerView.Adapter<RecipeListAdapter.ViewHolder>{
  2.  
  3. private List<Recipe> mRecipeSet;
  4. private Button mAddToGroceriesButton;
  5.  
  6.  
  7. public RecipeListAdapter(List<Recipe> recipes){
  8. mRecipeSet = recipes;
  9. }
  10.  
  11. // Provide a reference to the views for each data item
  12. // Complex data items may need more than one view per item, and
  13. // you provide access to all the views for a data item in a view holder
  14. public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  15. //This is what will handle what happens when you click a recipe in the recycler view
  16.  
  17.  
  18. private TextView mRecipeName;
  19. private TextView mPrepTime;
  20. private TextView mCookTime;
  21. private TextView mServingSize;
  22. private RelativeLayout mRecipeTextSection;
  23.  
  24.  
  25. public ViewHolder(View v) {
  26. super(v);
  27. mRecipeName = (TextView) v.findViewById(R.id.recipe_list_recycler_view_recipe_name);
  28. mServingSize = (TextView) v.findViewById(R.id.recipe_list_recycler_view_serving_size);
  29. mPrepTime = (TextView) v.findViewById(R.id.recipe_list_recycler_view_prep_time);
  30. mCookTime = (TextView) v.findViewById(R.id.recipe_list_recycler_view_cook_time);
  31. mRecipeTextSection = (RelativeLayout) v.findViewById(R.id.recycled_item_section_view);
  32.  
  33. mRecipeTextSection.setOnClickListener(this);
  34.  
  35. mAddToGroceriesButton = (Button) v.findViewById(R.id.add_to_grocery_list);
  36. mAddToGroceriesButton.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. int position = getAdapterPosition();
  40. Recipe recipeToGrocery = mRecipeSet.get(position);
  41.  
  42. //RecipeDB dbHelper = new RecipeDB(v.getContext());
  43. //dbHelper.addGroceryItem(recipeToGrocery);
  44.  
  45. if(!recipeToGrocery.isInList()) {
  46. RecipeDB dbHelper = new RecipeDB(v.getContext());
  47. dbHelper.addGroceryItem(recipeToGrocery);
  48.  
  49. recipeToGrocery.setInList(true);
  50. dbHelper.updateRecipe(recipeToGrocery);
  51. mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
  52. Toast.makeText(v.getContext(), recipeToGrocery.getRecipeName() + " added to grocery list.", Toast.LENGTH_SHORT).show();
  53. }
  54. else {
  55. Toast.makeText(v.getContext(), "That recipe is already in the list.", Toast.LENGTH_SHORT).show();
  56. }
  57.  
  58. }
  59. });
  60.  
  61. }
  62.  
  63. @Override
  64. public void onClick(View v){
  65. int position = getAdapterPosition();
  66. Intent i = new Intent(v.getContext(), RecipeTextView.class);
  67. Recipe selectedRecipe = mRecipeSet.get(position);
  68. i.putExtra("view_recipe_key", selectedRecipe);
  69. v.getContext().startActivity(i);
  70. }
  71.  
  72. }
  73.  
  74.  
  75. public void add(int position, Recipe item) {
  76. mRecipeSet.add(position, item);
  77. notifyItemInserted(position);
  78. }
  79.  
  80. public void remove(Recipe item) {
  81. int position = mRecipeSet.indexOf(item);
  82. mRecipeSet.remove(position);
  83. notifyItemRemoved(position);
  84. }
  85.  
  86.  
  87.  
  88. public RecipeListAdapter(ArrayList<Recipe> myRecipeset) {
  89. mRecipeSet = myRecipeset;
  90. }
  91.  
  92. // Create new views (invoked by the layout manager)
  93. @Override
  94. public RecipeListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  95. // create a new view
  96. View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item_recycled, parent, false);
  97. ViewHolder vh = new ViewHolder(v);
  98. return vh;
  99.  
  100. }
  101.  
  102.  
  103. @Override
  104. public void onBindViewHolder(ViewHolder holder, int position) {
  105. Recipe recipe = mRecipeSet.get(position);
  106. String recipeName = recipe.getRecipeName();
  107. String prepTime = "Prep Time: " + String.valueOf(recipe.getPrepTime()) + " minutes";
  108. String cookTime = "Cook Time: " + String.valueOf(recipe.getCookTime()) + " minutes";
  109. String servingSize = "Servings: " + String.valueOf(recipe.getServings());
  110.  
  111. holder.mRecipeName.setText(recipeName);
  112.  
  113. //Only display values if they are not null
  114. if(recipe.getServings() != null) {
  115. holder.mServingSize.setText(servingSize);
  116. }
  117. if (recipe.getPrepTime() != null) {
  118. holder.mPrepTime.setText(prepTime);
  119. }
  120. if(recipe.getCookTime() != null) {
  121. holder.mCookTime.setText(cookTime);
  122. }
  123.  
  124.  
  125. }
  126.  
  127. // Return the size of your dataset (invoked by the layout manager)
  128. @Override
  129. public int getItemCount() {
  130. if(mRecipeSet != null) {
  131. return mRecipeSet.size();
  132. }
  133. return 0;
  134. }
  135.  
  136.  
  137.  
  138. }
  139.  
  140. mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
  141.  
  142. if(recipe.isInList()){
  143. mAddToGroceriesButton.setBackgroundResource(R.mipmap.ic_playlist_add_check_black_24dp);
  144. }
  145.  
  146. <?xml version="1.0" encoding="utf-8"?>
  147. <RelativeLayout
  148. xmlns:android="http://schemas.android.com/apk/res/android"
  149. xmlns:tools="http://schemas.android.com/apk/res/android"
  150. android:layout_margin="7dp"
  151. android:orientation="vertical"
  152. android:layout_width="match_parent"
  153. android:layout_height="wrap_content"
  154. android:paddingLeft="10dp"
  155. android:id="@+id/recycled_item_section_view"
  156. android:elevation="30dp"
  157. android:background="@drawable/background_border"
  158. >
  159.  
  160.  
  161. <TextView
  162. android:layout_width="wrap_content"
  163. android:layout_height="wrap_content"
  164. tools:text="Recipe name"
  165. android:textSize="24dp"
  166. android:textColor="@color/black"
  167. android:id="@+id/recipe_list_recycler_view_recipe_name"
  168. android:paddingBottom="3dp"
  169. android:maxWidth="275dip"
  170. android:singleLine="false"/>
  171.  
  172. <TextView
  173. android:layout_width="match_parent"
  174. android:layout_height="wrap_content"
  175. android:textSize="18dp"
  176. android:textColor="@color/black"
  177. android:layout_below="@id/recipe_list_recycler_view_recipe_name"
  178. android:id="@+id/recipe_list_recycler_view_serving_size"
  179. android:paddingBottom="3dp"/>
  180.  
  181. <Button
  182. android:layout_width="35dp"
  183. android:layout_height="35dp"
  184. android:background="@mipmap/ic_playlist_add_black_24dp"
  185. android:height="36dp"
  186. android:padding="8dp"
  187. android:layout_alignParentRight="true"
  188. android:id="@+id/add_to_grocery_list"
  189. android:layout_alignParentTop="true"
  190. android:layout_alignParentEnd="true"
  191. />
  192.  
  193. <TextView
  194. android:layout_width="wrap_content"
  195. android:layout_height="wrap_content"
  196. android:layout_below="@id/recipe_list_recycler_view_serving_size"
  197. android:layout_alignParentLeft="true"
  198. android:textSize="18dp"
  199. android:textColor="@color/black"
  200. android:id="@+id/recipe_list_recycler_view_prep_time"
  201. android:paddingBottom="3dp"
  202. />
  203.  
  204. <TextView
  205. android:layout_width="wrap_content"
  206. android:layout_height="wrap_content"
  207. android:layout_below="@id/recipe_list_recycler_view_prep_time"
  208. android:textSize="18dp"
  209. android:textColor="@color/black"
  210. android:layout_alignParentLeft="true"
  211. android:id="@+id/recipe_list_recycler_view_cook_time"/>
  212.  
  213.  
  214. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement