Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GridAdapter extends BaseAdapter {
- private Context MyContext;
- public GridAdapter(Context _MyContext) {
- MyContext = _MyContext;
- }
- @Override
- public int getCount() {
- /* Set the number of element we want on the grid */
- //return listOfAllImages.size();
- return 20;
- }
- @Override
- public View getView(final int position, View convertView,
- ViewGroup parent) {
- View MyView = convertView;
- listOfAllImages = new ArrayList<String>();
- String absolutePathOfImage = null;
- Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
- String[] projection = { MediaColumns.DATA,
- MediaColumns.DISPLAY_NAME };
- cursor = BrowsePhotos.this.managedQuery(uri, projection, null,
- null, null);
- column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
- while (cursor.moveToNext()) {
- absolutePathOfImage = cursor.getString(column_index);
- listOfAllImages.add(absolutePathOfImage);
- startManagingCursor(cursor);
- }
- if (convertView == null) {
- /* we define the view that will display on the grid */
- // Inflate the layout
- // LayoutInflater li = getLayoutInflater();
- LayoutInflater li = (LayoutInflater) MyContext
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- MyView = li.inflate(R.layout.grid_item, null);
- // Add The Text!!!
- TextView tv = (TextView) MyView
- .findViewById(R.id.grid_item_text);
- tv.setText("Item " + position);
- // Add The Image!!!
- ImageView iv = (ImageView) MyView
- .findViewById(R.id.grid_item_image);
- iv.setImageURI(Uri.parse(listOfAllImages .get(position)));
- }
- for (int i = 1; i <=listOfAllImages.size(); i++) {
- System.out.println("Image" + listOfAllImages.get(position)
- + "??");
- }
- MyView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent i = new Intent(getBaseContext(), ViewImage.class);
- i.putExtra("position", position);
- startActivity(i);
- }
- });
- return MyView;
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
- }
- iv.setImageURI(Uri.parse(listOfAllImages .get(position)));
- 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): java.lang.IndexOutOfBoundsException: Invalid location 3, size is 3
- 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at java.util.ArrayList.get(ArrayList.java:341)
- 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at com.Xperia.BrowsePhotos$GridAdapter.getView(BrowsePhotos.java:134)
- 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
- 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at android.widget.GridView.makeAndAddView(GridView.java:1218)
- 07-27 10:12:15.211: ERROR/AndroidRuntime(3896): at android.widget.GridView.makeRow(GridView.java:265)
- public class GridAdapter extends BaseAdapter {
- private Context MyContext;
- public GridAdapter(Context _MyContext) {
- MyContext = _MyContext;
- listOfAllImages = new ArrayList<String>();
- String absolutePathOfImage = null;
- Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
- String[] projection = { MediaColumns.DATA,
- MediaColumns.DISPLAY_NAME };
- cursor = BrowsePhotos.this.managedQuery(uri, projection, null,
- null, null);
- column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
- while (cursor.moveToNext()) {
- absolutePathOfImage = cursor.getString(column_index);
- listOfAllImages.add(absolutePathOfImage);
- startManagingCursor(cursor);
- }
- }
- @Override
- public int getCount() {
- /* Set the number of element we want on the grid */
- return listOfAllImages.size();
- }
- @Override
- public View getView(final int position, View convertView,
- ViewGroup parent) {
- View MyView = convertView;
- if (convertView == null) {
- /* we define the view that will display on the grid */
- // Inflate the layout
- // LayoutInflater li = getLayoutInflater();
- LayoutInflater li = (LayoutInflater) MyContext
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- MyView = li.inflate(R.layout.grid_item, null);
- // Add The Text!!!
- TextView tv = (TextView) MyView
- .findViewById(R.id.grid_item_text);
- tv.setText("Item " + position);
- // Add The Image!!!
- ImageView iv = (ImageView) MyView
- .findViewById(R.id.grid_item_image);
- iv.setImageURI(Uri.parse(listOfAllImages .get(position)));
- }
- for (int i = 1; i <=listOfAllImages.size(); i++) {
- System.out.println("Image" + listOfAllImages.get(position)
- + "??");
- }
- MyView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent i = new Intent(getBaseContext(), ViewImage.class);
- i.putExtra("position", position);
- startActivity(i);
- }
- });
- return MyView;
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
- }
- note: getCount() must return listOfAllImages.size();
Add Comment
Please, Sign In to add comment