Advertisement
Guest User

Untitled

a guest
May 28th, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 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. import android.view.LayoutInflater;
  28. import android.view.View;
  29. import android.view.ViewGroup;
  30. import android.widget.TextView;
  31.  
  32. public class SimpleCursorRecyclerAdapter extends CursorRecyclerAdapter<SimpleViewHolder> {
  33.  
  34. private int mLayout;
  35. private int[] mFrom;
  36. private int[] mTo;
  37. private String[] mOriginalFrom;
  38.  
  39. public SimpleCursorRecyclerAdapter (int layout, Cursor c, String[] from, int[] to) {
  40. super(c);
  41. mLayout = layout;
  42. mTo = to;
  43. mOriginalFrom = from;
  44. findColumns(c, from);
  45. }
  46.  
  47. @Override
  48. public SimpleViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
  49. View v = LayoutInflater.from(parent.getContext())
  50. .inflate(mLayout, parent, false);
  51. return new SimpleViewHolder(v, mTo);
  52. }
  53.  
  54. @Override
  55. public void onBindViewHolder (SimpleViewHolder holder, Cursor cursor) {
  56. final int count = mTo.length;
  57. final int[] from = mFrom;
  58.  
  59. for (int i = 0; i < count; i++) {
  60. holder.views[i].setText(cursor.getString(from[i]));
  61. }
  62. }
  63.  
  64. /**
  65. * Create a map from an array of strings to an array of column-id integers in cursor c.
  66. * If c is null, the array will be discarded.
  67. *
  68. * @param c the cursor to find the columns from
  69. * @param from the Strings naming the columns of interest
  70. */
  71. private void findColumns(Cursor c, String[] from) {
  72. if (c != null) {
  73. int i;
  74. int count = from.length;
  75. if (mFrom == null || mFrom.length != count) {
  76. mFrom = new int[count];
  77. }
  78. for (i = 0; i < count; i++) {
  79. mFrom[i] = c.getColumnIndexOrThrow(from[i]);
  80. }
  81. } else {
  82. mFrom = null;
  83. }
  84. }
  85.  
  86. @Override
  87. public Cursor swapCursor(Cursor c) {
  88. findColumns(c, mOriginalFrom);
  89. return super.swapCursor(c);
  90. }
  91. }
  92.  
  93. class SimpleViewHolder extends RecyclerView.ViewHolder
  94. {
  95. public TextView[] views;
  96.  
  97. public SimpleViewHolder (View itemView, int[] to)
  98. {
  99. super(itemView);
  100. views = new TextView[to.length];
  101. for(int i = 0 ; i < to.length ; i++) {
  102. views[i] = (TextView) itemView.findViewById(to[i]);
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement