[英]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.