简体   繁体   中英

Listview custom adapter with imageview and buttons

I tried to make a custom array adapter for my listview but the emulator crashes always and I can't see where my code is good and where it isn't.

The logcat says "java.lang.OutOfMemoryError" but I don't know how to solve. I tried to modify the studio.exe.vmoptions file or removing images (even if they are only 50x50) from the whole app, without any effort.

So I'll post my code, asking your help for making order in my app.

Thank you in advance!

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

And here's a snippet of my code that calls the custom 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);
        }

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

also i dont think you need this, since you should read the data from the adapter itself

 for (i=0; i < FullMenu.size(); i++) {
        p = FullMenu.get(i);
        adapter.add(p);
    } 

you can check this sample i have here for an adapter https://github.com/juangdiaz/CoffeeApp/blob/master/app/src/main/java/com/juangdiaz/coffeeapp/adapter/ListAdapter.java

and calling the adapter here https://github.com/juangdiaz/CoffeeApp/blob/master/app/src/main/java/com/juangdiaz/coffeeapp/fragments/CoffeeListFragment.java

let me know if this helps

You should be resize dynamically your image, so you avoid a failure of memory.

You can read this lesson

And here you can see a simple example of its operation

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM