Checking if ListView is empty

13,565

Solution 1

Hey guys I solved my problems, I just add a EmptyView. So when the listview becomes empty it automatically shows an emptyview like this

ListCartAdapter adapter = new ListCartAdapter(cart.this, orderid, orderName, orderSize, orderQuantity);
    listView.setAdapter(adapter);
listView.setEmptyView(findViewById(R.id.emptyView));

Thanks to everyone that helped :D

Solution 2

Just check if adapter.getCount() == 0

Solution 3

You can use following to do same

listView.getAdapter().getItemCount(); 

To incorporate this, you need to make a listener for deleting the items and implement in your activity. Then in that override method, you can use above code.

Solution 4

just check if (cartlist.size()<0) then yours list is Empty.!

Solution 5

check if adapter.isEmpty() if it returns true the list is empty if it returns false then it isn't empty

Share:
13,565
Gionne Lapuz
Author by

Gionne Lapuz

Updated on June 27, 2022

Comments

  • Gionne Lapuz
    Gionne Lapuz about 2 years

    I am using SQLITE to store, retrieve and delete the values added in the cart. And added a delete button for each row in a Custom Listview.

    So when the user taps the delete button on the specific row it will delete the data also in the table.

    My problem is that how would I be able to check if the listview is empty when the user deletes everything in the cart?

    Can someone point me to what should I be doing or using? Thanks in Advance! :D

    this is my Cart Class:

      //CART LISTVIEW
    private ArrayList<String> orderid;
    private ArrayList<String> orderName;
    private ArrayList<String> orderSize;
    private ArrayList<String> orderQuantity;
    
    private cartDatabaseHelper db;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
    
        db = new cartDatabaseHelper(this);
    
        //CART LISTVIEW
        orderid = new ArrayList<>();
        orderName = new ArrayList<>();
        orderSize = new ArrayList<>();
        orderQuantity = new ArrayList<>();
    
        TextView textEmpty = (TextView) findViewById(R.id.textEmpty);
    
        Button btnCheckout = (Button) findViewById(R.id.btnCheckout);
    
        ListView listView = (ListView) findViewById(R.id.listView);
    
        //ListView listView1 = (ListView) findViewById(R.id.emptyView);
    
    
        final ListCartAdapter adapter = new ListCartAdapter(cart.this, orderid, orderName, orderSize, orderQuantity);
        listView.setAdapter(adapter);
    
    
        Cursor data = db.getListContents();
        data.moveToFirst();
    
        if(data.getCount() == 0){
            textEmpty.setVisibility(View.VISIBLE);
            btnCheckout.setVisibility(View.GONE);
        }
        else
        {
            textEmpty.setVisibility(View.GONE);
            btnCheckout.setVisibility(View.VISIBLE);
    
            data.moveToFirst();
            do{
                orderid.add(data.getString(0));
                orderName.add(data.getString(1));
                orderSize.add(data.getString(2));
                orderQuantity.add(data.getString(3));
            } while (data.moveToNext());
    
        }
        data.close();
    }
    
    
    @Override
    public void onBackPressed() {
        finish();
    }
    

    and this is my Adapter Class:

    public class ListCartAdapter extends BaseAdapter {
    
    
    private Context context;
    
    private ArrayList<String> orderid;
    private ArrayList<String> orderName;
    private ArrayList<String> orderSize;
    private ArrayList<String> orderQuantity;
    
    public ListCartAdapter(Context context, ArrayList<String> orderid, ArrayList<String> orderName, ArrayList<String> orderSize, ArrayList<String> orderQuantity){
        this.context = context;
        this.orderid = orderid;
        this.orderName = orderName;
        this.orderSize = orderSize;
        this.orderQuantity = orderQuantity;
    
    }
    
    @Override
    public int getCount() {
        return orderName.size();
    }
    
    @Override
    public Object getItem(int position) {
        return orderName.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return 0;
    }
    
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    
        final View listView;
        final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        listView = inflater.inflate(R.layout.cart_list_item, null);
    
        TextView number = (TextView) listView.findViewById(R.id.textID);
        TextView name = (TextView) listView.findViewById(R.id.textOrderName);
        TextView size = (TextView) listView.findViewById(R.id.textOrderSize);
        TextView quantity = (TextView) listView.findViewById(R.id.textOrderQuantity);
    
        ImageButton btnRemove = (ImageButton) listView.findViewById(R.id.btnRemove);
    
        number.setText(orderid.get(position));
        name.setText(orderName.get(position));
        size.setText(orderSize.get(position));
        quantity.setText(orderQuantity.get(position));
    
        final cartDatabaseHelper db = new cartDatabaseHelper(context);
    
    
       //BUTTON TO REMOVE ROW
       btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
    
                new AlertDialog.Builder(context)
                        .setTitle("REMOVE" + " " + orderName.get(position) + " " + "FROM CART?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
    
                                    db.removeSingleContact(orderid.get(position));
    
                                    orderName.remove(position);
                                    orderSize.remove(position);
                                    orderQuantity.remove(position);
                                    notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
    
                                dialog.dismiss();
                            }
                        })
                        .show();
            }
        });
        return listView;
    }