Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. import android.content.Context;
  2. import android.database.Cursor;
  3. import android.net.Uri;
  4. import android.support.v7.widget.CardView;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11.  
  12. import com.squareup.picasso.Picasso;
  13.  
  14. import java.util.Random;
  15.  
  16.  
  17. public class RVAdapter extends RecyclerViewCursorAdapter<RVAdapter.ProductViewHolder>
  18. {
  19. private static final String TAG = RVAdapter.class.getSimpleName();
  20. private final Context mContext;
  21. private final Random mRandom;
  22.  
  23.  
  24. public RVAdapter(Context context, String locationSetting)
  25. {
  26. super(null);
  27. mContext = context;
  28. mRandom = new Random(System.currentTimeMillis());
  29.  
  30. // Sort order: Ascending, by date.
  31. String sortOrder = ProductContract.ProductEntry.COLUMN_DATE + " ASC";
  32. Uri productForLocationUri = ProductContract.ProductEntry
  33. .buildProductLocationWithStartDate(locationSetting, System.currentTimeMillis());
  34.  
  35. // Students: Uncomment the next lines to display what what you stored in the bulkInsert
  36. Cursor cursor = mContext.getContentResolver()
  37. .query(productForLocationUri, null, null, null, sortOrder);
  38.  
  39. swapCursor(cursor);
  40. }
  41.  
  42. @Override
  43. public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
  44. {
  45. View view = LayoutInflater.from(parent.getContext())
  46. .inflate(R.layout.item, parent, false);
  47. return new ProductViewHolder(view);
  48. }
  49.  
  50. @Override
  51. protected void onBindViewHolder(ProductViewHolder holder, Cursor cursor)
  52. {
  53. String imagePath = cursor.getString(ShopFragment.COL_PRODUCT_IMAGE);
  54. String price = cursor.getString(ShopFragment.COL_PRODUCT_PRICE);
  55.  
  56. holder.productPrice.setText("US $" + price);
  57.  
  58. int height = mRandom.nextInt(50) + 500;
  59.  
  60. //Download image using picasso library
  61. Picasso.with(mContext)
  62. .load(imagePath)
  63. .resize(500, height)
  64. .error(R.drawable.placeholder)
  65. .placeholder(R.drawable.placeholder)
  66. .into(holder.productPhoto);
  67. }
  68.  
  69.  
  70. public static class ProductViewHolder extends RecyclerView.ViewHolder
  71. {
  72. CardView cv;
  73. TextView productPrice;
  74. ImageView productPhoto;
  75.  
  76. ProductViewHolder(View itemView)
  77. {
  78. super(itemView);
  79. cv = (CardView) itemView.findViewById(R.id.cv);
  80. productPrice = (TextView) itemView.findViewById(R.id.product_price);
  81. productPhoto = (ImageView) itemView.findViewById(R.id.product_photo);
  82. }
  83. }
  84. }
  85.  
  86. import android.database.Cursor;
  87. import android.database.DataSetObserver;
  88. import android.support.v7.widget.RecyclerView;
  89. import android.view.ViewGroup;
  90.  
  91.  
  92. /**
  93. * RecyclerView CursorAdapter
  94. * <p>
  95. * Created by Simon on 28/02/2016.
  96. */
  97. public abstract class RecyclerViewCursorAdapter<VH extends RecyclerView.ViewHolder> extends
  98. RecyclerView.Adapter<VH>
  99. {
  100. private Cursor mCursor;
  101. private boolean mDataValid;
  102. private int mRowIDColumn;
  103.  
  104.  
  105. public RecyclerViewCursorAdapter(Cursor cursor)
  106. {
  107. setHasStableIds(true);
  108. swapCursor(cursor);
  109. }
  110.  
  111. public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
  112.  
  113. protected abstract void onBindViewHolder(VH holder, Cursor cursor);
  114.  
  115. @Override
  116. public void onBindViewHolder(VH holder, int position)
  117. {
  118. if(!mDataValid){
  119. throw new IllegalStateException("this should only be called when the cursor is valid");
  120. }
  121. if(!mCursor.moveToPosition(position)){
  122. throw new IllegalStateException("couldn't move cursor to position " + position);
  123. }
  124. onBindViewHolder(holder, mCursor);
  125. }
  126.  
  127. @Override
  128. public long getItemId(int position)
  129. {
  130. if(mDataValid && mCursor != null && mCursor.moveToPosition(position)){
  131. return mCursor.getLong(mRowIDColumn);
  132. }
  133. return RecyclerView.NO_ID;
  134. }
  135.  
  136. @Override
  137. public int getItemCount()
  138. {
  139. if(mDataValid && mCursor != null){
  140. return mCursor.getCount();
  141. }
  142. else{
  143. return 0;
  144. }
  145. }
  146.  
  147. protected Cursor getCursor()
  148. {
  149. return mCursor;
  150. }
  151.  
  152. public void changeCursor(Cursor cursor)
  153. {
  154. Cursor old = swapCursor(cursor);
  155. if(old != null){
  156. old.close();
  157. }
  158. }
  159.  
  160. public Cursor swapCursor(Cursor newCursor)
  161. {
  162. if(newCursor == mCursor){
  163. return null;
  164. }
  165. Cursor oldCursor = mCursor;
  166. if(oldCursor != null){
  167. if(mDataSetObserver != null){
  168. oldCursor.unregisterDataSetObserver(mDataSetObserver);
  169. }
  170. }
  171. mCursor = newCursor;
  172. if(newCursor != null){
  173. if(mDataSetObserver != null){
  174. newCursor.registerDataSetObserver(mDataSetObserver);
  175. }
  176. mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
  177. mDataValid = true;
  178. notifyDataSetChanged();
  179. }
  180. else{
  181. mRowIDColumn = -1;
  182. mDataValid = false;
  183. notifyDataSetChanged();
  184. }
  185. return oldCursor;
  186. }
  187.  
  188.  
  189. private DataSetObserver mDataSetObserver = new DataSetObserver()
  190. {
  191. @Override
  192. public void onChanged()
  193. {
  194. mDataValid = true;
  195. notifyDataSetChanged();
  196. }
  197.  
  198. @Override
  199. public void onInvalidated()
  200. {
  201. mDataValid = false;
  202. notifyDataSetChanged();
  203. }
  204. };
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement