Android 清除链接到 SQLite 的列表视图

Android clear listview linked to SQLite

我有以下 activity 从 SQLite 填充列表视图。 单击 empty 时,我会正确删除 table 内容并刷新显示为空的 CartActivity。 如果我在我的购物车中添加新元素,那么旧元素会再次出现,但只会出现在列表视图中,并且(正确地)不会出现在我的数据库中。如果我关闭应用程序,列表视图会正确更新。

试图查看类似的问题,尝试 notify 但没有任何反应。有人写了关于 "you delete the data but not the entries" 但我不能。

我该如何解决这个问题?

如有任何帮助,我们将不胜感激。提前致谢。

public class CartActivity extends Activity {
ListView list;
Context context;
SessionManagement session;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
    Typeface font = Typeface.createFromAsset(this.getAssets(), "font/VarelaRound-Regular.ttf");
    context = getApplicationContext();
    session = new SessionManagement(context);
    final CartHandler db = new CartHandler(this);
    Log.d("Reading: ", "Reading all contacts..");
    final List<CartRow> products = db.getAllProducts();

    for (CartRow cn : products) {
        String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Number: " + cn.getNumber() + " ,Pieces: " + cn.getPieces() + " ,Price: " + cn.getPrice() + " ,Tot: " + cn.getTotPrice();
        Log.d("Nome: ", log);
    }
    if(products.isEmpty() ){
    //Intent intent = new Intent(getApplicationContext(),MenuActivity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        //startActivity(intent);
        Toast.makeText(this, "", Toast.LENGTH_LONG).show();
        //finish();
    } else {
        final CartHandler mdb = new CartHandler(this);
        getItemsFromDatabase(mdb);
        final CustomCarterList adapter = new CustomCarterList(CartActivity.this);
        list = (ListView) findViewById(R.id.cart);
        list.setAdapter(adapter);
        Button empty = (Button) findViewById(R.id.emptycart);
        empty.setTypeface(font);
        empty.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                db.deleteAll();
                //mdb.deleteAll();
                Intent intent = new Intent(CartActivity.this, CartActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
        });
        Button proceed = (Button) findViewById(R.id.proceed);
        proceed.setTypeface(font);
        proceed.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!session.isLoggedIn()) {
                    Intent intent = new Intent(CartActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                } else {
                    Intent intent = new Intent(
                            getApplicationContext(),
                            CheckoutActivity.class
                    );
                    startActivity(intent);
                }
            }
        });

    }

}

    public void getItemsFromDatabase(CartHandler mdb) {
        Cursor cursor = null;
        try{
            SQLiteDatabase db =mdb.getReadableDatabase();
            cursor=db.rawQuery("select * from products", null);
            while (cursor.moveToNext()){
                Log.e("Cart", cursor.getString(1)+":"+cursor.getString(2)+":"+cursor.getString(3)+":"+cursor.getString(4)+":"+cursor.getString(5));
                CartRow.itemIdList.add(cursor.getString(0));
                CartRow.itemNameList.add(cursor.getString(1));
                CartRow.itemQuantityList.add(cursor.getString(2));
                if (cursor.getString(3).equals("0")){
                    CartRow.itemPiecesList.add("Pieces: 1");}
                else{
                CartRow.itemPiecesList.add("Pieces: "+cursor.getString(3));}
                CartRow.itemPriceList.add(cursor.getString(4) + ".00€");
                CartRow.itemTotPriceList.add(cursor.getString(5)+".00€");
            }
            cursor.close();
        }catch (SQLException e){
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }
    }

使用 adapter.notifyDataSetChanged() 而不是通知。

像这样更改 onCreate 方法的 else 部分...

if(products.isEmpty() ){
    //Intent intent = new Intent(getApplicationContext(),MenuActivity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        //startActivity(intent);
        Toast.makeText(this, "", Toast.LENGTH_LONG).show();
        //finish();
    } else {
        final CartHandler mdb = new CartHandler(this);
        getItemsFromDatabase(mdb);
        final CustomCarterList adapter = new CustomCarterList(CartActivity.this);
        list = (ListView) findViewById(R.id.cart);
        list.setAdapter(adapter);
        Button empty = (Button) findViewById(R.id.emptycart);
        empty.setTypeface(font);
        empty.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                db.deleteAll();
                CartRow.itemIdList.clear();   // clears the list
                adapter.notifyDataSetChanged();  // notifies adapter about the change.
                //mdb.deleteAll();
                Intent intent = new Intent(CartActivity.this, CartActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
        });
        Button proceed = (Button) findViewById(R.id.proceed);
        proceed.setTypeface(font);
        proceed.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!session.isLoggedIn()) {
                    Intent intent = new Intent(CartActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                } else {
                    Intent intent = new Intent(
                            getApplicationContext(),
                            CheckoutActivity.class
                    );
                    startActivity(intent);
                }
            }
        });

    }

已解决: 呼叫

CartRow.itemIdList.clear();
CartRow.itemNameList.clear(); 
CartRow.itemNumberList.clear();
CartRow.itemPiecesList.clear();
CartRow.itemPriceList.clear();
CartRow.itemTotPriceList.clear();

之前

getItemsFromDatabase(mdb);

及之后

db.deleteAll();