2016-01-14 123 views
0

我有自定義ArrayAdapter:簡單的佈局膨脹 - 的OutOfMemoryError

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

我的產品:

public class PostPreviewItem { 
    String imageUrl; 
    String title; 
    String description; 

    } 

我不包括在圖像中的所有適配器測試的ArrayList的。 它可能是什麼原因? 感謝

這裏是我的xml:

<ImageView 
     android:id="@+id/preview_post_image" 
     android:layout_width="100dp" 
     android:layout_height="50dp" 
     android:layout_marginLeft="@dimen/regular_corner_small" 
     android:layout_marginTop="@dimen/regular_top" 
     > 
    </ImageView> 

    <TextView 
     android:id="@+id/preview_post_title" 
     android:textStyle="bold" 
     android:layout_toRightOf="@+id/preview_post_image" 
     android:layout_width="wrap_content" 
     android:textColor="@color/color_black" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/regular_top" 
     android:textSize="16sp" > 
    </TextView> 

    <TextView 
     android:id="@+id/preview_post_description" 
     android:layout_below="@+id/preview_post_title" 
     android:layout_toRightOf="@+id/preview_post_image" 
     android:layout_width="wrap_content" 
     android:textColor="@color/color_black" 
     android:layout_height="wrap_content" 
     android:textSize="12sp" > 
    </TextView> 

+0

我猜你的佈局沒有優化。發佈你的XML請 –

+0

'ArrayList mPostPreviewItems'中存儲了多少個元素? –

+0

@Rikki請發佈整個堆棧跟蹤。 –

回答

0

我認爲這是由於畫面 的不正確處理,並建議您使用ImageLoader或相同的庫