我的列表視圖適配器出現問題。在這裏我的源代碼如下的適配器:列表視圖適配器中的java.lang.outofmemoryerror異常
public class ListItemAdapter extends BaseAdapter {
private static ArrayList<ItemDetail_class> DataProcessorResult;
private LayoutInflater myInflater;
Context mycontext;
public ListItemAdapter(Context context, ArrayList<ItemDetail_class> results) {
DataProcessorResult = results;
myInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return DataProcessorResult.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return DataProcessorResult.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final Context mycontext=parent.getContext();
if(convertView == null) {
convertView = myInflater.inflate(R.layout.custom_view_item, null);
holder = new ViewHolder();
holder.tvitemcode = (TextView) convertView.findViewById(R.id.tvitemcode);
holder.tvitemname = (TextView) convertView.findViewById(R.id.tvitemname);
holder.tvsize = (TextView) convertView.findViewById(R.id.tvsize);
holder.tvprice = (TextView) convertView.findViewById(R.id.tvprice);
holder.imgitem= (ImageView) convertView.findViewById(R.id.imgitem);
holder.imgview= (ImageView) convertView.findViewById(R.id.imgview);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tvitemcode.setText(DataProcessorResult.get(position).getItemCode());
holder.tvitemname.setText(DataProcessorResult.get(position).getItemName());
holder.tvsize.setText(DataProcessorResult.get(position).getSize());
holder.tvprice.setText(DataProcessorResult.get(position).getPrice());
holder.tvitemcode.setTextSize(TypedValue.COMPLEX_UNIT_PX, mycontext.getResources().getDimensionPixelSize(R.dimen.lbllistviewitem));
holder.tvitemname.setTextSize(TypedValue.COMPLEX_UNIT_PX, mycontext.getResources().getDimensionPixelSize(R.dimen.lbllistviewitem));
holder.tvsize.setTextSize(TypedValue.COMPLEX_UNIT_PX, mycontext.getResources().getDimensionPixelSize(R.dimen.lbllistviewitem));
holder.tvprice.setTextSize(TypedValue.COMPLEX_UNIT_PX, mycontext.getResources().getDimensionPixelSize(R.dimen.lbllistviewitem));
String imgName = DataProcessorResult.get(position).getItemCode();
imgName=(imgName.replace("-",""));
imgName=imgName.toLowerCase();
int imgID=0;
holder.imgitem.getLayoutParams().width= mycontext.getResources().getDimensionPixelSize(R.dimen.photolistviewitem);
holder.imgitem.getLayoutParams().height= mycontext.getResources().getDimensionPixelSize(R.dimen.photolistviewitem);
if (mycontext.getResources().getIdentifier(imgName+"_1", "drawable", mycontext.getPackageName())==0)
{
String imagepath = Environment.getExternalStorageDirectory() + "/Asiades/"+imgName+".jpg";
File FileMain = new File(imagepath);
if (FileMain.exists()){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(imagepath, options);
holder.imgitem.setImageBitmap(bitmap);
}else{
imgID = mycontext.getResources().getIdentifier("blankitem", "drawable", mycontext.getPackageName());
holder.imgitem.setImageResource(imgID);
}
}else {
imgID = mycontext.getResources().getIdentifier(imgName+"_1", "drawable", mycontext.getPackageName());
holder.imgitem.setImageResource(imgID);
}
holder.imgview.getLayoutParams().width= mycontext.getResources().getDimensionPixelSize(R.dimen.arrowicon);
holder.imgview.getLayoutParams().height= mycontext.getResources().getDimensionPixelSize(R.dimen.arrowicon);
holder.imgview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
OtherClass.setItemCode(DataProcessorResult.get(position).getItemCode());
Intent MyIntent=new Intent(mycontext, ViewDetailItem.class);
mycontext.startActivity(MyIntent);
}
});
if (position % 2 == 1) {convertView.setBackgroundColor(Color.WHITE);} else {convertView.setBackgroundColor(Color.rgb(208,212,208)); }
return convertView;
}
static class ViewHolder {
TextView tvitemcode;
TextView tvitemname;
TextView tvsize;
TextView tvprice;
ImageView imgitem;
ImageView imgview;
}
}
這裏我logcat的錯誤: 10-30 00:12:38.370:E/AndroidRuntime(22719):在android.content.res.Resources.loadDrawable(資源。 java:3022) 10-30 00:12:38.370:E/AndroidRuntime(22719):at android.content.res.Resources.getDrawable(Resources.java:1586) 10-30 00:12:38.370:E/AndroidRuntime(22719):在android.widget.ImageView.resolveUri(ImageView.java:648) 10-30 00:12:38.370:E/AndroidRuntime(22719):at android.widget.ImageView.setImageResource(ImageView.java: 377)
有人可以幫我解決這個問題會徽?非常感謝您的支持