开发者

Invalid location 3,size is 3?

开发者 https://www.devze.com 2023-03-23 09:04 出处:网络
In my android app I have a gridview that I want to show images from my sdcard. I stored the path of images in arraylist and now I want to show the image in the gridview by taking from uri from the arr

In my android app I have a gridview that I want to show images from my sdcard. I stored the path of images in arraylist and now I want to show the image in the gridview by taking from uri from the arraylist.

This is my code :

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 = c开发者_开发技巧onvertView;

            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;
        }
    }

In listOfALlImages I have all the path of images, but at line

iv.setImageURI(Uri.parse(listOfAllImages .get(position)));

I got this in logCat:

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)

On sdcard I have 3 images.

Can anyone help me? I really don't understand where is my mistake.

Thanks in advance.


In your getCount() method you should return listOfAllImages.size().

The indexing is zero based. So get(0) return the first item, get(1) the second and so on.

0

精彩评论

暂无评论...
验证码 换一张
取 消