在删除其中一个项目后,如何立即更新listView中显示的每个项目的总价?

时间:2015-04-28 17:30:06

标签: java android listview android-listview

我有一个listView,它从我现有的数据库中显示购物车中的服装项目,在底部显示我购物车中所有商品的总价。当我从购物车中删除商品时,listView确实会更新,它会删除该商品,但是当我删除此商品时,底部显示的总价格不会更新,而此总价格仅来自执行SUM的查询(价格)从我的表中包含我的购物车中的商品。我在下面发布了我的一些代码,如果有人可以提供帮助,那就太棒了。

DatabaseHelper.java



package ankitkaushal.app.healthysizing;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

public class DatabaseHelper extends SQLiteOpenHelper {

    public static String DB_PATH = "/data/data/ankitkaushal.app.healthysizing/databases/";
    public static String DB_NAME = "FinishDatabase";
    public static final int DB_VERSION = 1;
    public static final String shoppingCartTable = "OPS_insert";
    private SQLiteDatabase myDB;
    private Context context;

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }


    @Override
    public synchronized void close(){
        if(myDB!=null){
            myDB.close();
        }
        super.close();
    }


    // Retrieves all shirt and pant items of the specified brand typed into the search bar
    public ArrayList<Item> getAllSearchedItems(String brand, String table_name, String size) {

        if (size.equals("null")) {

            brand = "\"" + brand + "\"";
            ArrayList<Item> itemList = new ArrayList<Item>();
            String query = "SELECT * FROM " + table_name + " WHERE Brand LIKE " + brand + " ORDER BY Price ASC"; //query to get all the shirt or pant items for brand typed in
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(query, null);

            if (cursor.moveToFirst()){
                do {
                    Item item = new Item();
                    item.setBrand(cursor.getString(0));
                    item.setStore(cursor.getString(1));
                    item.setPrice(cursor.getString(2));
                    item.setDescription(cursor.getString(3));
                    item.setSize(cursor.getString(4));
                    item.setGender(cursor.getString(5));
                    item.setID(cursor.getString(6));
                    itemList.add(item);
                } while (cursor.moveToNext());
            }

            return itemList;

        }

        else {

            brand = "\"" + brand + "\"";
            ArrayList<Item> itemList = new ArrayList<Item>();
            String query = "SELECT * FROM " + table_name + " WHERE Brand LIKE " + brand + " AND Size = '" + size + "' ORDER BY Price ASC"; //query to get all the shirt or pant items for brand typed in
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(query, null);

            if (cursor.moveToFirst()){
                do {
                    Item item = new Item();
                    item.setBrand(cursor.getString(0));
                    item.setStore(cursor.getString(1));
                    item.setPrice(cursor.getString(2));
                    item.setDescription(cursor.getString(3));
                    item.setSize(cursor.getString(4));
                    item.setGender(cursor.getString(5));
                    item.setID(cursor.getString(6));
                    itemList.add(item);
                } while (cursor.moveToNext());
            }

            return itemList;

        }

    }


    // Retrieves all shirt or pant items from the database
    public ArrayList<Item> getAllItems(String table_name, String size) {

        if (size.equals("null")) {

            ArrayList<Item> itemList = new ArrayList<Item>();
            String query = "SELECT * FROM " + table_name + " ORDER BY Price ASC"; //query to get all the shirt or pant items
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(query, null);

            if (cursor.moveToFirst()){
                do {
                    Item item = new Item();
                    item.setBrand(cursor.getString(0));
                    item.setStore(cursor.getString(1));
                    item.setPrice(cursor.getString(2));
                    item.setDescription(cursor.getString(3));
                    item.setSize(cursor.getString(4));
                    item.setGender(cursor.getString(5));
                    item.setID(cursor.getString(6));
                    itemList.add(item);
                } while (cursor.moveToNext());
            }

            return itemList;

        }

        else {

            ArrayList<Item> itemList = new ArrayList<Item>();
            String query = "SELECT * FROM " + table_name + " WHERE Size = '" + size + "' ORDER BY Price ASC"; //query to get all the shirt or pant items
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(query, null);

            if (cursor.moveToFirst()){
                do {
                    Item item = new Item();
                    item.setBrand(cursor.getString(0));
                    item.setStore(cursor.getString(1));
                    item.setPrice(cursor.getString(2));
                    item.setDescription(cursor.getString(3));
                    item.setSize(cursor.getString(4));
                    item.setGender(cursor.getString(5));
                    item.setID(cursor.getString(6));
                    itemList.add(item);
                } while (cursor.moveToNext());
            }

            return itemList;

        }

    }

    // Retrieves all shoe items from the database
    public ArrayList<Item> getAllShoes() {

        ArrayList<Item> shoeList = new ArrayList<Item>();
        String query = "SELECT * FROM Shoes ORDER BY Price ASC "; // query to get all shoe items
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        if (cursor.moveToFirst()){
            do {
                Item item = new Item();
                item.setBrand(cursor.getString(0));
                item.setStore(cursor.getString(1));
                item.setPrice(cursor.getString(2));
                item.setDescription(cursor.getString(3));
                item.setGender(cursor.getString(4));
                item.setID(cursor.getString(5));
                item.setSize("See Description");
                shoeList.add(item);
            } while (cursor.moveToNext());
        }

        return shoeList;

    }


    // Retrieves all shoe items of specified brand typed into the search bar
    public ArrayList<Item> getAllSearchedShoes(String brand) {

        brand = "\"" + brand + "\"";
        ArrayList<Item> shoeList = new ArrayList<Item>();
        String query = "SELECT * FROM Shoes WHERE Brand LIKE " + brand + " ORDER BY Price ASC "; //query to get all the shoes for brand typed in
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        if (cursor.moveToFirst()){
            do {
                Item item = new Item();
                item.setBrand(cursor.getString(0));
                item.setStore(cursor.getString(1));
                item.setPrice(cursor.getString(2));
                item.setDescription(cursor.getString(3));
                item.setGender(cursor.getString(4));
                item.setID(cursor.getString(5));
                item.setSize("See Description");
                shoeList.add(item);
            } while (cursor.moveToNext());
        }

        return shoeList;

    }


    // Retrieves all outerwear items from the database
    public ArrayList<Item> getAllOuterwear() {

        ArrayList<Item> outerwearList = new ArrayList<Item>();
        String query = "SELECT * FROM Outerwear ORDER BY Price ASC "; // query to get all outerwear items
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        if (cursor.moveToFirst()){
            do {
                Item item = new Item();
                item.setBrand(cursor.getString(0));
                item.setStore(cursor.getString(1));
                item.setPrice(cursor.getString(2));
                item.setDescription(cursor.getString(3));
                item.setSize(cursor.getString(4));
                item.setGender(cursor.getString(5));
                item.setID(cursor.getString(6));
                outerwearList.add(item);
            } while (cursor.moveToNext());
        }

        return outerwearList;

    }


    // Retrieves all outerwear items of specified brand typed into the search bar
    public ArrayList<Item> getAllSearchedOuterwear(String brand) {

        brand = "\"" + brand + "\"";
        ArrayList<Item> outerwearList = new ArrayList<Item>();
        String query = "SELECT * FROM Outerwear WHERE Brand LIKE " + brand + " ORDER BY Price ASC "; //query to get all the outerwear for brand typed in
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        if (cursor.moveToFirst()){
            do {
                Item item = new Item();
                item.setBrand(cursor.getString(0));
                item.setStore(cursor.getString(1));
                item.setPrice(cursor.getString(2));
                item.setDescription(cursor.getString(3));
                item.setSize(cursor.getString(4));
                item.setGender(cursor.getString(5));
                item.setID(cursor.getString(6));
                outerwearList.add(item);
            } while (cursor.moveToNext());
        }

        return outerwearList;

    }


    // Retrieves all the items in the cart
    public ArrayList<Item> getItemsInCart() {

        ArrayList<Item> cartList = new ArrayList<Item>();
        String query = "SELECT * FROM " + shoppingCartTable + " ORDER BY Price ASC"; // query to get all the items in the cart
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        if (cursor.moveToFirst()){
            do {
                Item item = new Item();
                item.setBrand(cursor.getString(0));
                item.setStore(cursor.getString(1));
                item.setPrice(cursor.getString(2));
                item.setDescription(cursor.getString(3));
                item.setSize(cursor.getString(4));
                item.setGender(cursor.getString(5));
                item.setID(cursor.getString(6));
                cartList.add(item);
            } while (cursor.moveToNext());
        }

        return cartList;

    }


    // Adds the item chosen from the listView to your shopping cart.
    public void addToCart(Item item) {

        String description = item.getDescription();
        description = description.replaceAll("\"", " inch");
        String brand = item.getBrand();
        description = "\"" + description + "\"";
        brand = "\"" + brand + "\"";
        String query = "INSERT INTO " + shoppingCartTable + " (Brand, Store, Price, Description, Size, Gender, ID) " +
                "VALUES (" + brand + ", '" + item.getStore() + "', '" + item.getPrice() + "', "
                + description + ", '" + item.getSize() + "', '" + item.getGender() + "', '" + item.getID() + "')";
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(query);

    }


    // Deletes the item from the shopping cart
    public void deleteItemFromCart(String ID) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(shoppingCartTable, " ID = '" + ID + "'", null);
    }


    // Returns total price of items in shopping cart
    public String getTotalCartPrice() {
        String SQLQuery = "SELECT SUM(Price) FROM OPS_insert";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c = db.rawQuery(SQLQuery, null);
        c.moveToFirst();
        String priceTotal = c.getString(0);
        return priceTotal;
    }


    public void setLimitPants(String startSize, String currentSize, String limit) {
        String SQLQuery = "UPDATE setLimitPants SET startingSize = '" + startSize + "', currentSize = '" + currentSize + "', limitSize = '" + limit + "'";
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(SQLQuery);
    }


    public String getLimit(String tableName) {
        String SQLQuery = "SELECT limitSize FROM " + tableName;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c = db.rawQuery(SQLQuery, null);
        c.moveToFirst();
        String limit = c.getString(0);
        return limit;
    }


    public void setLimitShirts(String startSize, String currentSize, String limit) {
        String SQLQuery = "UPDATE setLimitShirts SET startingSize = '" + startSize + "', currentSize = '" + currentSize + "', limitSize = '" + limit + "'";
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(SQLQuery);
    }


    public String getStartSizePants() {
        String SQLQuery = "SELECT startingSize FROM setLimitPants";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c = db.rawQuery(SQLQuery, null);
        c.moveToFirst();
        String startingSize = c.getString(0);
        return startingSize;
    }


    public String getStartSizeShirts() {
        String SQLQuery = "SELECT startingSize FROM setLimitShirts";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c = db.rawQuery(SQLQuery, null);
        c.moveToFirst();
        String startingSize = c.getString(0);
        return startingSize;
    }


    public String getCurrentShirtSize() {
        String SQLQuery = "SELECT currentSize FROM setLimitShirts";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c = db.rawQuery(SQLQuery, null);
        c.moveToFirst();
        String currentSize = c.getString(0);
        return currentSize;
    }


    public String getCurrentPantsSize() {
        String SQLQuery = "SELECT currentSize FROM setLimitPants";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c = db.rawQuery(SQLQuery, null);
        c.moveToFirst();
        String currentSize = c.getString(0);
        return currentSize;
    }


    public void updateSizeShirts(String newSize) {
        String SQLQuery = "UPDATE setLimitShirts SET currentSize = '" + newSize + "'";
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(SQLQuery);
    }


    public void updateSizePants(String newSize) {
        String SQLQuery = "UPDATE setLimitPants SET currentSize = '" + newSize + "'";
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(SQLQuery);
    }


    public void removeLImit(String tableName) {
        String SQLQuery = "UPDATE " + tableName + " SET startingSize = 'null', currentSize = 'null', limitSize = 'null'";
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(SQLQuery);
    }


    /***
     * Open database
     * @throws android.database.SQLException
     */
    public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }


    /**
     * Copy database from source code assets to device
     * @throws java.io.IOException
     */
    public void copyDataBase() throws IOException {
        try {
            InputStream myInput = context.getAssets().open(DB_NAME);
            String outputFileName = DB_PATH + DB_NAME;
            OutputStream myOutput = new FileOutputStream(outputFileName);

            byte[] buffer = new byte[1024];
            int length;

            while((length = myInput.read(buffer))>0){
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
            myOutput.close();
            myInput.close();
        } catch (Exception e) {
            Log.e("tle99 - copyDatabase", e.getMessage());
        }

    }


    /***
     * Check if the database doesn't exist on device, create new one
     * @throws IOException
     */
    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();

        if (dbExist) {

        } else {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                Log.e("tle99 - create", e.getMessage());
            }
        }
    }


    // ---------------------------------------------
    // PRIVATE METHODS
    // ---------------------------------------------

    /***
     * Check if the database is exist on device or not
     * @return
     */
    private boolean checkDataBase() {
        SQLiteDatabase tempDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            tempDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLiteException e) {
            Log.e("tle99 - check", e.getMessage());
        }
        if (tempDB != null)
            tempDB.close();
        return tempDB != null ? true : false;
    }

}
&#13;
&#13;
&#13;

CartItemsAdapter.java

&#13;
&#13;
package ankitkaushal.app.healthysizing;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public final class CartItemsAdapter extends ArrayAdapter<Item> implements View.OnClickListener {

    public CartItemsAdapter(Context context, ArrayList<Item> shirtItems) {
        super(context, 0, shirtItems);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        Item item = getItem(position);

        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.cart_layout, parent, false);
        }

        // Lookup view for data population
        TextView brand = (TextView) convertView.findViewById(R.id.txt_cart_brand);
        TextView price = (TextView) convertView.findViewById(R.id.txt_cart_price);
        TextView store = (TextView) convertView.findViewById(R.id.txt_cart_store);
        TextView size = (TextView) convertView.findViewById(R.id.txt_cart_size);
        TextView description = (TextView) convertView.findViewById(R.id.txt_cart_description);
        ImageView shirtsImage = (ImageView) convertView.findViewById(R.id.image_view_cart);
        Button deleteFromCartButton = (Button) convertView.findViewById(R.id.deleteItemButton);

        // Populate the data into the template view using the data object
        brand.setText("Brand:" + "  " + item.getBrand());
        price.setText("Price:" + "  $" + item.getPrice());
        store.setText("Store:" + "  " + item.getStore());
        size.setText("Size:" + "  " + item.getSize());
        description.setText("Description:" + "  " + item.getDescription());

        Context context = parent.getContext();

        try { // Find the image name from database ID column and link that up with the name of image in drawable folder
            String itemName = item.getID();
            String uri = "@drawable/"+itemName;
            int imageResource = context.getResources().getIdentifier(uri, null, context.getApplicationContext().getPackageName());
            Drawable drawable = context.getResources().getDrawable(imageResource);
            shirtsImage.setImageDrawable(drawable);
        }

        catch (Exception e) { // If no image found for item, just set the image to a default image in drawable folder
            // TODO Auto-generated catch block
            Drawable drawable = context.getResources().getDrawable(R.drawable.shirts); // Default image
            shirtsImage.setImageDrawable(drawable);
        }

        deleteFromCartButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                // To get the item from the listView for which the Add to Cart button is pressed
                Item item = getItem(position);

                // Delete the item from the cart by pressing the delete item button
                DatabaseHelper db = new DatabaseHelper(getContext());
                db.deleteItemFromCart(item.getID());
                remove(item);

                // Update the listView to reflect the changes
                notifyDataSetChanged();

            }

        });

        // Return the completed view to render on screen
        return convertView;

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}
&#13;
&#13;
&#13;

shoppingCart.java

&#13;
&#13;
package ankitkaushal.app.healthysizing;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;

public class shoppingCart extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_cart);

        final DatabaseHelper dbhelper;
        final ListView listView;
        final ListAdapter cartAdapter;

        dbhelper = new DatabaseHelper(getApplicationContext());
        try {
            dbhelper.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }

        listView = (ListView) findViewById(R.id.itemsInCartList);
        ArrayList<Item> cartList = dbhelper.getItemsInCart();

        if (cartList != null) {
            cartAdapter = new CartItemsAdapter(getApplicationContext(), cartList);
            listView.setAdapter(cartAdapter);
        }

        listView.setEmptyView(findViewById(R.id.emptyCartMessage));

        TextView displayTotalPrice = (TextView) findViewById(R.id.totalCartPrice);
        String totalCartPrice = dbhelper.getTotalCartPrice();

        if (totalCartPrice != null) {
            displayTotalPrice.setText("Total Price: $" + totalCartPrice);
        }

        else {
            displayTotalPrice.setText("Total Price: $0.00");
        }

    }

}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

在我看来,最快的方法是:

  1. 创建:OnItemDeletedListener.java
  2. OnItemDeletedListener.java

    public interface OnItemDeletedListener {
        public void onItemDeleted();
    }
    
      CartItemsAdapter添加以下代码
    1. private OnItemDeletedListener onItemDeletedListener;   
      
      public void setOnItemDeletedListener(Object object) {
          onItemDeletedListener = (OnItemDeletedListener) object;
      }
      
    2. 在功能getView(...)中将setOnClickListener替换为:

      deleteFromCartButton.setOnClickListener(new View.OnClickListener() {
      
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
      
                  // To get the item from the listView for which the Add to Cart button is pressed
                  Item item = getItem(position);
      
                  // Delete the item from the cart by pressing the delete item button
                  DatabaseHelper db = new DatabaseHelper(getContext());
                  db.deleteItemFromCart(item.getID());
                  remove(item);
      
                  // Update the listView to reflect the changes
                  notifyDataSetChanged();
                  onItemDeletedListener.onItemDeleted();
              }
      
    3. 更改shoppingCart课程:

    4. shoppingCart.java

      package ankitkaushal.app.healthysizing;
      
      import android.support.v7.app.ActionBarActivity;
      import android.os.Bundle;
      import android.widget.ListAdapter;
      import android.widget.ListView;
      import android.widget.TextView;
      import java.io.IOException;
      import java.util.ArrayList;
      
       public class shoppingCart extends ActionBarActivity implements OnItemDeletedListener {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_shopping_cart);
      
              final DatabaseHelper dbhelper;
              final ListView listView;
              final ListAdapter cartAdapter;
      
              dbhelper = new DatabaseHelper(getApplicationContext());
              try {
                  dbhelper.createDataBase();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      
              listView = (ListView) findViewById(R.id.itemsInCartList);
              ArrayList<Item> cartList = dbhelper.getItemsInCart();
      
              if (cartList != null) {
                  cartAdapter = new CartItemsAdapter(getApplicationContext(), cartList);
                  cartAdapter.setOnItemDeletedListener(this);
                  listView.setAdapter(cartAdapter);
              }
      
              listView.setEmptyView(findViewById(R.id.emptyCartMessage));
      
              displayTotalPrice();
          }
      
          private void displayTotalPrice() {
      
              TextView displayTotalPrice = (TextView) findViewById(R.id.totalCartPrice);
              String totalCartPrice = dbhelper.getTotalCartPrice();
      
              if (totalCartPrice != null) {
                  displayTotalPrice.setText("Total Price: $" + totalCartPrice);
              }
      
              else {
                  displayTotalPrice.setText("Total Price: $0.00");
              }
          }
      
          @Override
          public void onItemDeleted() {
              displayTotalPrice();
          }
      }
      

      [编辑]

      final ListAdapter cartAdapter;替换为final CartItemsAdapter cartAdapter;

答案 1 :(得分:0)

您单击删除按钮时似乎没有通知displayTotalPrice(如果这是您从购物车中删除项目时要更新的TextView)。您的活动应该听取CartItemsAdapter中所做的更改。

将此内部界面添加到CartItemsAdapter。

public static interface ItemDeleteListener {
    public void onItemDelete(double price);
}

然后在CartItemsAdapter中引用此接口:

ItemDeleteListener itemDeleteListener;

当您创建新的CartItemsAdapter(将其作为参数传递)时,应初始化此变量:

// 'this' in this case is your Activity passed into Adapter as an argument
cartAdapter = new CartItemsAdapter(getApplicationContext(), cartList, this);

在CartItemsAdapter构造函数中,然后初始化该接口:

this.itemDeleteListener = listener;

然后让您的购物车Activity实现此界面:

public class shoppingCart extends ActionBarActivity implements CartItemsAdapter.ItemDeleteListener {
    ...
    @Override
    public void onItemDelete(doublePrice) {

        // here you notify displayTotalPrice, doublePrice should be total price
        displayTotalPrice.setText(doublePrice);

        }
}

发生更改时,请在CartItemsAdapter中调用它,如下所示:

itemDeleteListener.onItemDelete(db.getTotalCartPrice());