简体   繁体   English

将图像作为ArrayList传递 <Bitmap> 从片段到自定义列表适配器

[英]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) : 片段类(在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 class (Inside doInBackground method of AsyncTask class) : 获取图像类(在AsyncTask类的doInBackground方法内部):

            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) : 自定义适配器类(在getView方法内部):

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

I am new to android development and it is so much fun! 我是android开发的新手,它非常有趣! ^.^ 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. 我在错误的地方调用了notifydatasetchanged(),即在将图像下载到后台线程之前。 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). 由于我的getCount()返回了图像数..到我调用notifydatasetchanged()时,尚未填充ArrayList(即,后台线程未完成)。 Hope it helps someone with similer error sometime. 希望它能在某个时候帮助出现类似错误的人。 thanks everyone for help! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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