检查ListView是否为空

时间:2017-05-09 06:05:06

标签: android sqlite listview android-adapter baseadapter

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

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

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

有人能指出我应该做什么或使用什么?提前致谢! :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;
}

5 个答案:

答案 0 :(得分:6)

只需检查<post-creator post-id="5" category="1"></post-creator> <script src="components/post-creator/post-creator.component.js"></script>

答案 1 :(得分:3)

您可以使用以下内容来执行相同的操作

listView.getAdapter().getItemCount(); 

要合并此内容,您需要创建一个侦听器来删除项目并在您的活动中实现。然后在该覆盖方法中,您可以使用上面的代码。

答案 2 :(得分:2)

只检查是否$,然后您的列表为空。!

答案 3 :(得分:1)

检查 adapter.isEmpty(),如果它返回 true ,如果列表返回 false 则列表为空,那么它不是

答案 4 :(得分:0)

嘿伙计们我解决了我的问题,我只是添加一个EmptyView。因此,当listview变空时,它会自动显示像这样的空视图

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

感谢所有帮助过的人:D