我正在使用SQLITE来存储,检索和删除购物车中添加的值。并为自定义Listview中的每一行添加了一个删除按钮。

因此,当用户点击特定行上的删除按钮时,它将在表中也删除数据。

我的问题是,当用户删除购物车中的所有内容时,如何查看列表视图是否为空?

有人可以指出我该怎么做或使用吗?提前致谢! :D

这是我的购物车类别:

  //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();
}


这是我的适配器类:

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

最佳答案

只需检查adapter.getCount() == 0

关于android - 检查ListView是否为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43862252/

10-09 15:41