简体   繁体   中英

Passing images as ArrayList<Bitmap> from Fragment to Custom List Adapter

I am not able to send bitmap data from fragment class to custom adapter class. The log show me the following error.

FATAL EXCEPTION: main
Process: com.example.readersden, PID: 21025 
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
atjava.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.readersden.CardAdapter.getView(CardAdapter.java:82)

Fragment class (Inside onPostExecute method of AsyncTask) :

            Bitmap.Config conf = Bitmap.Config.ARGB_8888; 

            Bitmap bmp = Bitmap.createBitmap(w, h, conf);
            try {
                JSONObject imageInfo = volumeObject.getJSONObject("imageLinks");
                new GetBookThumb().execute(imageInfo.getString("thumbnail"));
            } catch (JSONException je) {
                bookThumb.add(0, bmp);
                je.printStackTrace();
            }

Fetch Image class (Inside doInBackground method of AsyncTask class) :

            InputStream thumbIn = thumbConn.getInputStream();
            BufferedInputStream thumbBuff = new BufferedInputStream(thumbIn);
            thumbImg = BitmapFactory.decodeStream(thumbBuff);
            bookThumb.add(0, thumbImg);
            thumbBuff.close();
            thumbIn.close();

Custom Adapter Class (Inside getView method) :

            ArrayList<Bitmap> bookThumb;
            e.thumb.setImageBitmap(bookThumb.get(pos));

I am new to android development and it is so much fun! ^.^ thank you!

Edit :

public CardAdapter(ArrayList<String> bookTitle,
        ArrayList<String> bookAuthor, ArrayList<String> bookPublishDate,
        ArrayList<String> bookPages, ArrayList<Bitmap> bookThumb,
        Activity mAct) {
        super();
        this.bookTitle = bookTitle;
        this.bookAuthor = bookAuthor;
        this.bookPublishDate = bookPublishDate;
        this.bookPages = bookPages;
        this.bookThumb = bookThumb;
        this.mAct = mAct;

        mInflater = (LayoutInflater) mAct
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public View getView(int pos, View view, ViewGroup viewGroup) {


        LayoutField e;
            if (view == null) {
            view = mInflater.inflate(R.layout.layout_card, viewGroup, false);
            e = new LayoutField();
            e.title = (TextView) view.findViewById(R.id.book_title);
            e.thumb = (ImageView) view.findViewById(R.id.book_thumbnail);
            e.author = (TextView) view.findViewById(R.id.book_author);
            e.publishDate = (TextView) view
                .findViewById(R.id.book_publish_date);
            e.pages = (TextView) view.findViewById(R.id.book_pages);
            view.setTag(e);
        } else
            e = (LayoutField) view.getTag();

        e.title.setText(bookTitle.get(pos));
        e.thumb.setImageBitmap(bookThumb.get(pos));
        e.author.setText(bookAuthor.get(pos));
        e.publishDate.setText(bookPublishDate.get(pos));
        e.pages.setText(bookPages.get(pos));

        return view;
    }

    class LayoutField {
        TextView title, author, publishDate, pages;
        ImageView thumb;
    }
}

Passing text is working perfectly fine! just the images give index out of bounds.

I was calling the notifydatasetchanged() at the wrong place ie before the images were downloaded in the background thread. since my getCount() was returning the number of images.. by the time i called notifydatasetchanged() the ArrayList was not populated (ie background thread not completed). Hope it helps someone with similer error sometime. thanks everyone for help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM