簡體   English   中英

將圖像作為ArrayList傳遞 <Bitmap> 從片段到自定義列表適配器

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

我無法將位圖數據從片段類發送到自定義適配器類。 日志顯示以下錯誤。

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)

片段類(在AsyncTask的onPostExecute方法內部):

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

獲取圖像類(在AsyncTask類的doInBackground方法內部):

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

自定義適配器類(在getView方法內部):

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

我是android開發的新手,它非常有趣! ^。^謝謝!

編輯:

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

傳遞文字效果很好! 僅圖像給出了超出范圍的索引。

我在錯誤的地方調用了notifydatasetchanged(),即在將圖像下載到后台線程之前。 由於我的getCount()返回了圖像數..到我調用notifydatasetchanged()時,尚未填充ArrayList(即,后台線程未完成)。 希望它能在某個時候幫助出現類似錯誤的人。 謝謝大家的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM