我试图为我的listview创建一个自定义数组适配器,但模拟器总是崩溃,我看不到我的代码在哪里好,哪些不好。
logcat说“java.lang.OutOfMemoryError”,但我不知道如何解决。我尝试修改studio.exe.vmoptions文件或从整个应用程序中删除图像(即使它们只有50x50),而不费力。
所以我会发布我的代码,请求你帮我在我的应用程序中订购。
提前谢谢!
public class MyClassAdapter extends ArrayAdapter<Plate> {
private static class ViewHolder {
TextView Id;
ImageView Image;
TextView Name;
TextView Description;
TextView Type;
TextView Cost;
TextView Count;
TextView Comment;
Button Buttonup;
Button Buttondown;
}
public MyClassAdapter(Context context, int textViewResourceId, ArrayList<Plate> items) {
super(context, textViewResourceId, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Plate item = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.item_main, parent, false);
viewHolder = new ViewHolder();
viewHolder.Id = (TextView)convertView.findViewById(R.id.code);
viewHolder.Image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.Name = (TextView) convertView.findViewById(R.id.name);
viewHolder.Description = (TextView) convertView.findViewById(R.id.description);
viewHolder.Cost = (TextView) convertView.findViewById(R.id.price);
viewHolder.Count = (TextView) convertView.findViewById(R.id.count);
viewHolder.Buttonup = (Button) convertView.findViewById(R.id.button_up);
viewHolder.Buttondown = (Button) convertView.findViewById(R.id.button_down);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (item!= null) {
viewHolder.Id.setText(String.format("%d",item.getId()));
viewHolder.Image.setImageURI(item.getImage());
viewHolder.Name.setText(String.format("%s", item.getName()));
viewHolder.Description.setText(String.format("%s", item.getDescription()));
viewHolder.Name.setText(String.format("%s", item.getName()));
viewHolder.Cost.setText(String.format("%s", item.getCost()));
viewHolder.Count.setText(String.format("%d", item.getCount()));
viewHolder.Buttonup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DBHelper mydb= new DBHelper(getContext());
mydb.AddPlate(item.getId());
item.CountUp();
//update viewholder.Count
}
});
viewHolder.Buttondown.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
DBHelper mydb= new DBHelper(getContext());
mydb.RemovePlate(item.getId());
item.CountDown();
//update viewholder.Count
}
});
}
return convertView;
}
这是我的代码片段,它调用自定义ArrayAdapter
ArrayList<Plate> FullMenu;
FullMenu = mydb.getPlates("Entrees");
Plate p;
int i;
MyClassAdapter adapter = new MyClassAdapter(this,0,FullMenu);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
for (i=0; i < FullMenu.size(); i++) {
p = FullMenu.get(i);
adapter.add(p);
}
答案 0 :(得分:0)
移动setadapter
ArrayList<Plate> FullMenu;
FullMenu = mydb.getPlates("Entrees");
Plate p;
int i;
MyClassAdapter adapter = new MyClassAdapter(this,0,FullMenu);
ListView listView = (ListView) findViewById(R.id.list);
for (i=0; i < FullMenu.size(); i++) {
p = FullMenu.get(i);
adapter.add(p);
}
listView.setAdapter(adapter);
我也不认为你需要这个,因为你应该从适配器本身读取数据
for (i=0; i < FullMenu.size(); i++) {
p = FullMenu.get(i);
adapter.add(p);
}
你可以查看我在这里找到适配器的样本 https://github.com/juangdiaz/CoffeeApp/blob/master/app/src/main/java/com/juangdiaz/coffeeapp/adapter/ListAdapter.java
如果有帮助请告诉我
答案 1 :(得分:0)
您应该调整大小 动态您的图片,这样可以避免内存失败。
祝你好运!