2015-04-21 52 views
0

我有一個listView從我現有的數據庫中填充,並顯示每個項目我也有一個刪除按鈕,以便我可以刪除它,當我這樣做時,我希望屏幕刷新並顯示更新listView與該項目已被刪除。如何在使用notifyDataSetChanged方法刪除項目後刷新我的listView?

據我所知,我應該使用notifyDataSetChanged方法,但它並沒有伎倆。我也嘗試過在線解決這個問題,但沒有任何效果,所以我在下面發佈了一些我的代碼,如果有人能幫助,那會很好。

CartItemsAdapter.java

package ankitkaushal.app.healthysizing; 

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

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

       // 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 

    } 

} 

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 { 

    @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"); 
     } 

    } 

} 

回答

0
deleteFromCartButton.setOnClickListener(new View.OnClickListener() { 

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

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

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

        // Remove item 
        remove(item); 

        // Update the listView to reflect the changes 
        notifyDataSetChanged(); 
       } 
} 
deleteFromCartButton.setTag(getItem(position)); 
+0

看起來很簡單,但沒有什麼被刷新... –

+0

@RayKing你有什麼問題? –

+0

nvm,我不得不將remove方法添加到我的自定義片段類中。現在一切正常 –

1

從後臺列表對象中刪除的項目。

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 
      remove(item); 

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

}); 
+0

onClick方法中的getItem(position)不是獲取Item對象的不好方法嗎? –

+0

似乎是使用'ArrayAdapter'進行操作的唯一方法。 – tachyonflux

+0

我不這麼認爲。我們可以使用視圖類的標籤來存儲對象。稍後我們可以檢索它。 –

相關問題