我有自定义ArrayAdapter:
TabPage
}
并接收public class PostPreviewAdapter extends ArrayAdapter<PostPreviewItem> {
private Context mContext;
private ArrayList<PostPreviewItem> mPostPreviewItems;
int resource;
public PostPreviewAdapter(Context context, int resource, ArrayList<PostPreviewItem> itemArrayList) {
super(context, resource, itemArrayList);
mContext = context;
this.resource = resource;
mPostPreviewItems = itemArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(resource, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.titleTextView = (TextView) convertView.findViewById(R.id.preview_post_title);
viewHolder.descriptionTextView = (TextView) convertView.findViewById(R.id.preview_post_description);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.preview_post_image);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolderItem) convertView.getTag();
}
// object item based on the position
PostPreviewItem postPreviewItem = getItem(position);
// assign values if the object is not null
if(postPreviewItem != null) {
// get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
viewHolder.titleTextView.setText(postPreviewItem.getTitle());
viewHolder.descriptionTextView.setText(postPreviewItem.getDescription());
if (postPreviewItem.getImageUrl()!=null & !postPreviewItem.getImageUrl().isEmpty())
Picasso.with(mContext).load(postPreviewItem.getImageUrl()).into(viewHolder.imageView);
}
return convertView;
}
static class ViewHolderItem {
TextView titleTextView;
TextView descriptionTextView;
ImageView imageView;
}
给布局充气:
OutOfMemoryError
我按照以下方式初始化它:
convertView = inflater.inflate(resource, parent, false);
我的项目是:
PostPreviewAdapter adapter = new PostPreviewAdapter(getActivity(), R.layout.post_preview_item,arrayList);
我根本没有在适配器的ArrayList中包含图像以进行测试。 它可能是什么原因? 谢谢
这是我的xml:
public class PostPreviewItem {
String imageUrl;
String title;
String description;
}