Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if ListView is empty

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;
}
like image 592
Gionne Lapuz Avatar asked Oct 25 '25 03:10

Gionne Lapuz


2 Answers

Just check if adapter.getCount() == 0

like image 79
Cristan Avatar answered Oct 27 '25 17:10

Cristan


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.

like image 34
Aashish Aadarsh Avatar answered Oct 27 '25 17:10

Aashish Aadarsh