【问题标题】:Passing images as ArrayList<Bitmap> from Fragment to Custom List Adapter将图像作为 ArrayList<Bitmap> 从片段传递到自定义列表适配器
【发布时间】:2014-09-16 00:20:29
【问题描述】:

我无法将位图数据从片段类发送到自定义适配器类。日志显示以下错误。

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

Fetch Image 类(在 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;
    }
}

传递文本工作得很好!只是图像给出了超出范围的索引。

【问题讨论】:

  • 需要更多代码。您如何将 arrayList 分配给 bookThumb ?
  • *.get(pos) 数组之一没有任何元素。
  • 嗨!我正在使用 bookThumb.add(index, bitmap); 将位图图像分配给 arraylist;方法。
  • @nija 它的 arraylist 将索引抛出边界错误。可能它的 null 或数据存储在多个索引中?我需要检查任何例外情况吗?如果是这样..在哪里?
  • @rogerthatcode bookThumb 没有元素,因为异常显示size is 0。那就是问题所在。将日志记录添加到异常所在的位置以及添加到数组的位置以查看操作顺序?

标签: java android android-fragments arraylist bitmap


【解决方案1】:

我在错误的地方调用了 notifydatasetchanged(),即在后台线程中下载图像之前。因为我的 getCount() 正在返回图像的数量。当我调用 notifydatasetchanged() 时,ArrayList 尚未填充(即后台线程未完成)。希望它有时可以帮助有类似错误的人。谢谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    相关资源
    最近更新 更多