这是我的代码,它响应三星手机上的Outofmemory错误! 我在谷歌搜索非常多但没有回答。 在列表中显示图像时显示强制关闭。
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final String[] imageId;
public CustomList(Activity context,String[] web,String[] img) {
super(context, R.layout.tag_list_item, web);
this.context = context;
this.web = web;
this.imageId = img;
}
@SuppressLint({ "ViewHolder", "InflateParams" })
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.tag_list_item, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
try{
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier("cat_"+imageId[position].trim().toString(), "drawable",
context.getPackageName());
//resources.getDrawable(resourceId);
imageView.setImageDrawable(resources.getDrawable(resourceId));
}catch(Exception e){
Toast.makeText(getApplicationContext(), e.toString()+" "+imageId[position].trim().toString(), Toast.LENGTH_LONG).show();
}
return rowView;
}
}
答案 0 :(得分:0)
您应优化代码以防止每次滚动时重新创建行。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.tag_list_item, null);
viewHolder = new ViewHolder();
viewHolder.txtTitle =(TextView)convertView.findViewById(R.id.txt);
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.img);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag(); // this line doesn' t recreate the view but reuse the one created before
}
viewHolder.txtTitle.setText(web[position]);
try{
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier("cat_"+imageId[position].trim().toString(), "drawable",
context.getPackageName());
//resources.getDrawable(resourceId);
viewHolder.imageView.setImageDrawable(resources.getDrawable(resourceId));
}catch(Exception e){
Toast.makeText(getApplicationContext(), e.toString()+" "+imageId[position].trim().toString(), Toast.LENGTH_LONG).show();
}
return convertView;
}
private class ViewHolder {
public TextView txtTitle;
public ImageView imageview;
}
希望它有所帮助......快乐的编码:)
答案 1 :(得分:-1)
我在清单中使用了堆积并结束了。