Guest User

Untitled

a guest
Jul 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. public class GridAdapter extends BaseAdapter {
  2. private Context MyContext;
  3.  
  4. public GridAdapter(Context _MyContext) {
  5. MyContext = _MyContext;
  6. }
  7.  
  8. @Override
  9. public int getCount() {
  10. /* Set the number of element we want on the grid */
  11. //return listOfAllImages.size();
  12. return 20;
  13. }
  14.  
  15. @Override
  16. public View getView(final int position, View convertView,
  17. ViewGroup parent) {
  18. View MyView = convertView;
  19.  
  20. listOfAllImages = new ArrayList<String>();
  21. String absolutePathOfImage = null;
  22. Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  23.  
  24. String[] projection = { MediaColumns.DATA,
  25. MediaColumns.DISPLAY_NAME };
  26.  
  27. cursor = BrowsePhotos.this.managedQuery(uri, projection, null,
  28. null, null);
  29. column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
  30.  
  31. while (cursor.moveToNext()) {
  32. absolutePathOfImage = cursor.getString(column_index);
  33. listOfAllImages.add(absolutePathOfImage);
  34. startManagingCursor(cursor);
  35. }
  36.  
  37. if (convertView == null) {
  38. /* we define the view that will display on the grid */
  39.  
  40. // Inflate the layout
  41. // LayoutInflater li = getLayoutInflater();
  42. LayoutInflater li = (LayoutInflater) MyContext
  43. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  44. MyView = li.inflate(R.layout.grid_item, null);
  45.  
  46. // Add The Text!!!
  47. TextView tv = (TextView) MyView
  48. .findViewById(R.id.grid_item_text);
  49. tv.setText("Item " + position);
  50.  
  51. // Add The Image!!!
  52. ImageView iv = (ImageView) MyView
  53. .findViewById(R.id.grid_item_image);
  54. iv.setImageURI(Uri.parse(listOfAllImages .get(position)));
  55. }
  56.  
  57.  
  58. for (int i = 1; i <=listOfAllImages.size(); i++) {
  59. System.out.println("Image" + listOfAllImages.get(position)
  60. + "??");
  61. }
  62.  
  63.  
  64. MyView.setOnClickListener(new View.OnClickListener() {
  65.  
  66. @Override
  67. public void onClick(View view) {
  68. Intent i = new Intent(getBaseContext(), ViewImage.class);
  69. i.putExtra("position", position);
  70. startActivity(i);
  71. }
  72.  
  73. });
  74.  
  75.  
  76. return MyView;
  77. }
  78.  
  79. @Override
  80. public Object getItem(int arg0) {
  81. // TODO Auto-generated method stub
  82. return null;
  83. }
  84.  
  85. @Override
  86. public long getItemId(int position) {
  87. // TODO Auto-generated method stub
  88. return 0;
  89. }
  90. }
  91.  
  92. iv.setImageURI(Uri.parse(listOfAllImages .get(position)));
  93.  
  94. 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): java.lang.IndexOutOfBoundsException: Invalid location 3, size is 3
  95. 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at java.util.ArrayList.get(ArrayList.java:341)
  96. 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at com.Xperia.BrowsePhotos$GridAdapter.getView(BrowsePhotos.java:134)
  97. 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
  98. 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at android.widget.GridView.makeAndAddView(GridView.java:1218)
  99. 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at android.widget.GridView.makeRow(GridView.java:265)
  100.  
  101. public class GridAdapter extends BaseAdapter {
  102. private Context MyContext;
  103.  
  104. public GridAdapter(Context _MyContext) {
  105. MyContext = _MyContext;
  106. listOfAllImages = new ArrayList<String>();
  107. String absolutePathOfImage = null;
  108. Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  109.  
  110. String[] projection = { MediaColumns.DATA,
  111. MediaColumns.DISPLAY_NAME };
  112.  
  113. cursor = BrowsePhotos.this.managedQuery(uri, projection, null,
  114. null, null);
  115. column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
  116.  
  117. while (cursor.moveToNext()) {
  118. absolutePathOfImage = cursor.getString(column_index);
  119. listOfAllImages.add(absolutePathOfImage);
  120. startManagingCursor(cursor);
  121. }
  122. }
  123.  
  124. @Override
  125. public int getCount() {
  126. /* Set the number of element we want on the grid */
  127. return listOfAllImages.size();
  128. }
  129.  
  130. @Override
  131. public View getView(final int position, View convertView,
  132. ViewGroup parent) {
  133. View MyView = convertView;
  134.  
  135.  
  136.  
  137. if (convertView == null) {
  138. /* we define the view that will display on the grid */
  139.  
  140. // Inflate the layout
  141. // LayoutInflater li = getLayoutInflater();
  142. LayoutInflater li = (LayoutInflater) MyContext
  143. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  144. MyView = li.inflate(R.layout.grid_item, null);
  145.  
  146. // Add The Text!!!
  147. TextView tv = (TextView) MyView
  148. .findViewById(R.id.grid_item_text);
  149. tv.setText("Item " + position);
  150.  
  151. // Add The Image!!!
  152. ImageView iv = (ImageView) MyView
  153. .findViewById(R.id.grid_item_image);
  154. iv.setImageURI(Uri.parse(listOfAllImages .get(position)));
  155. }
  156.  
  157.  
  158. for (int i = 1; i <=listOfAllImages.size(); i++) {
  159. System.out.println("Image" + listOfAllImages.get(position)
  160. + "??");
  161. }
  162.  
  163.  
  164. MyView.setOnClickListener(new View.OnClickListener() {
  165.  
  166. @Override
  167. public void onClick(View view) {
  168. Intent i = new Intent(getBaseContext(), ViewImage.class);
  169. i.putExtra("position", position);
  170. startActivity(i);
  171. }
  172.  
  173. });
  174.  
  175.  
  176. return MyView;
  177. }
  178.  
  179. @Override
  180. public Object getItem(int arg0) {
  181. // TODO Auto-generated method stub
  182. return null;
  183. }
  184.  
  185. @Override
  186. public long getItemId(int position) {
  187. // TODO Auto-generated method stub
  188. return 0;
  189. }
  190. }
  191.  
  192. note: getCount() must return listOfAllImages.size();
Add Comment
Please, Sign In to add comment