Advertisement
Guest User

Untitled

a guest
May 28th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 ARNAUD FRUGIER
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24.  
  25. import android.database.Cursor;
  26. import android.support.v7.widget.RecyclerView;
  27.  
  28. public abstract class CursorRecyclerAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
  29.  
  30. protected boolean mDataValid;
  31. protected Cursor mCursor;
  32. protected int mRowIDColumn;
  33.  
  34. public CursorRecyclerAdapter(Cursor c) {
  35. init(c);
  36. }
  37.  
  38. void init(Cursor c) {
  39. boolean cursorPresent = c != null;
  40. mCursor = c;
  41. mDataValid = cursorPresent;
  42. mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
  43. setHasStableIds(true);
  44. }
  45.  
  46. @Override
  47. public final void onBindViewHolder (VH holder, int position) {
  48. if (!mDataValid) {
  49. throw new IllegalStateException("this should only be called when the cursor is valid");
  50. }
  51. if (!mCursor.moveToPosition(position)) {
  52. throw new IllegalStateException("couldn't move cursor to position " + position);
  53. }
  54.  
  55. onBindViewHolder(holder, mCursor);
  56. }
  57.  
  58. public abstract void onBindViewHolder(VH holder, Cursor cursor);
  59.  
  60. public Cursor getCursor() {
  61. return mCursor;
  62. }
  63.  
  64. @Override
  65. public int getItemCount () {
  66. if (mDataValid && mCursor != null) {
  67. return mCursor.getCount();
  68. } else {
  69. return 0;
  70. }
  71. }
  72.  
  73. @Override
  74. public long getItemId (int position) {
  75. if(hasStableIds() && mDataValid && mCursor != null){
  76. if (mCursor.moveToPosition(position)) {
  77. return mCursor.getLong(mRowIDColumn);
  78. } else {
  79. return RecyclerView.NO_ID;
  80. }
  81. } else {
  82. return RecyclerView.NO_ID;
  83. }
  84. }
  85.  
  86. /**
  87. * Change the underlying cursor to a new cursor. If there is an existing cursor it will be
  88. * closed.
  89. *
  90. * @param cursor The new cursor to be used
  91. */
  92. public void changeCursor(Cursor cursor) {
  93. Cursor old = swapCursor(cursor);
  94. if (old != null) {
  95. old.close();
  96. }
  97. }
  98.  
  99. /**
  100. * Swap in a new Cursor, returning the old Cursor. Unlike
  101. * {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
  102. * closed.
  103. *
  104. * @param newCursor The new cursor to be used.
  105. * @return Returns the previously set Cursor, or null if there wasa not one.
  106. * If the given new Cursor is the same instance is the previously set
  107. * Cursor, null is also returned.
  108. */
  109. public Cursor swapCursor(Cursor newCursor) {
  110. if (newCursor == mCursor) {
  111. return null;
  112. }
  113. Cursor oldCursor = mCursor;
  114. mCursor = newCursor;
  115. if (newCursor != null) {
  116. mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
  117. mDataValid = true;
  118. // notify the observers about the new cursor
  119. notifyDataSetChanged();
  120. } else {
  121. mRowIDColumn = -1;
  122. mDataValid = false;
  123. // notify the observers about the lack of a data set
  124. notifyItemRangeRemoved(0, getItemCount());
  125. }
  126. return oldCursor;
  127. }
  128.  
  129. /**
  130. * <p>Converts the cursor into a CharSequence. Subclasses should override this
  131. * method to convert their results. The default implementation returns an
  132. * empty String for null values or the default String representation of
  133. * the value.</p>
  134. *
  135. * @param cursor the cursor to convert to a CharSequence
  136. * @return a CharSequence representing the value
  137. */
  138. public CharSequence convertToString(Cursor cursor) {
  139. return cursor == null ? "" : cursor.toString();
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement