【问题标题】:Android clear listview linked to SQLiteAndroid clear listview 链接到 SQLite
【发布时间】:2015-06-05 17:52:25
【问题描述】:

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

尝试查看类似的问题,尝试使用notify,但没有任何反应。有人写过“你删除数据但不删除条目”,但我不能。

我该如何解决这个问题?

任何帮助将不胜感激。提前致谢。

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

【问题讨论】:

  • 您正在使用“db”两次引用购物车处理程序,第二次使用“mdb”,尝试在活动中引用一次并对其进行处理。
  • 尝试引用一次,然后调用adapter.notifyDataSetChanged()但没有
  • 我认为您应该在适配器中尝试 CursorAdapter,当我需要将列表视图与来自 sql 的数据连接时,我喜欢它们

标签: android sqlite listview android-listview custom-adapter


【解决方案1】:

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

【讨论】:

  • 试过了还是一样
【解决方案2】:

像这样更改 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);
                }
            }
        });

    }

【讨论】:

  • 如果可能,更改适配器的构造函数 (CustomCarterList) 并从 Activity 传递数据列表,而不是从 CartRow.itemIdList 获取数据;
  • 我想我不能这样做,列表是动态的,两个活动之间没有联系。尝试了您的代码,现在: 1- 清空购物车 2- 我添加 product_x -> 购物车正确填充 3- 更改活动,重新打开 CartActivity -> 我的购物车中有两行(但不在我的数据库中:product_x \n product_x 4- 我删除了我的购物车和我的数据库 5- 插入新产品 'product_y' 6- 重新打开购物车:我看到 product_x 7- 再次重新打开:我看到 product_x \n product_x 8- 再次重新打开:我看到 product_x \n product_x \n product_y
【解决方案3】:

已解决: 调用

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

之前

getItemsFromDatabase(mdb);

之后

db.deleteAll();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多