2017-06-15 43 views
1

我想顯示在我的recyclerview在textview.if新項目的總項目數增加或刪除textview應update.and也計算項目的總價格在一個回收站視圖中的項目列表,並顯示在回收站視圖下方的文本視圖中。顯示編號。在android工作室的textview中的recyclerview的項目

下面

是我recyclerview適配器:

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> { 

    private List<ProductView.Data> productData = Collections.emptyList(); 
    static List<ProductModel> productModelList; 
    static Context context; 
    DatabaseHandler mDatabaseHandler; 

    public CartAdapter(Context context, List<ProductModel> dbList){ 
     this.productModelList = new ArrayList<ProductModel>(); 
     this.context = context; 
     this.productModelList = dbList; 
     mDatabaseHandler = new DatabaseHandler(context); 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     Context context = parent.getContext(); 
     LayoutInflater inflater = LayoutInflater.from(context); 

     // Inflate the custom layout 
     View cartListView = inflater.inflate(R.layout.list_item_cart, parent, false); 

     // Return a new holder instance 
     ViewHolder viewHolder = new ViewHolder(context,cartListView); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, final int position) { 

     holder.tvProductName.setText(productModelList.get(position).getTitle()); 
     holder.tvProductPrice.setText(productModelList.get(position).getPrice()); 
     Glide 
       .with(context) 
       .load(productModelList.get(position).getImageUrl()) 
       .into(holder.imgProduct); 
//  holder.tvProductStatus.setText(productModelList.get(position).getIsAvailable()); 
     holder.tvSize.setText(productModelList.get(position).getSize()); 
     holder.tvProductQuantity.setText(Integer.toString(productModelList.get(position).getQuantity())); 
     holder.tvColor.setText(productModelList.get(position).getColor()); 

//   holder.tvMaterial.setText(productModelList.get(position).getMaterial()); 

     holder.imgDelete.setClickable(true); 
     holder.imgDelete.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String idForDelete = String.valueOf(productModelList.get(position).getVariantId()); 

       mDatabaseHandler.deleteARow(idForDelete); 
       productModelList.remove(position); 
       notifyItemRemoved(position); 
       notifyItemRangeChanged(position,productModelList.size()); 

      } 
     }); 
    } 

    @Override 
    public int getItemCount() { 

     return productModelList.size(); 
    } 



    public class ViewHolder extends RecyclerView.ViewHolder { 

     TextView tvProductName, tvProductPrice, tvProductQuantity,tvColor, 
       tvSize; 
     ImageView imgProduct; 
     ImageButton imgDelete; 
     Context context; 

     public ViewHolder(Context mContext, View itemView) { 
      super(itemView); 

      this.tvProductName = (TextView) itemView.findViewById(R.id.tv_cart_product_name); 
      this.tvProductPrice = (TextView) itemView.findViewById(R.id.tv_cart_product_price); 
      this.tvProductQuantity = (TextView) itemView.findViewById(R.id.tv_cart_product_Quantity); 
      this.imgProduct = (ImageView) itemView.findViewById(R.id.img_cart_item_product); 
      this.tvColor = (TextView)itemView.findViewById(R.id.tv_color); 
      this.tvSize = (TextView) itemView.findViewById(R.id.tv_size); 
      this.imgDelete = (ImageButton) itemView.findViewById(R.id.img_cart_delete); 
      // store the context /// 
      this.context = mContext; 
     } 

    } 

和Java類:

公共類CartActivity擴展AppCompatActivity {

DatabaseHandler helper; 
List<ProductModel> dbList; 
RecyclerView mRecyclerView; 
Toolbar toolbar; 
Button btnCheckout, btnContinueShopping; 
TextView tvTotalNoOfItems, tvTotalPrice; 
String p; 
String i; 

private RecyclerView.Adapter mAdapter; 
private RecyclerView.LayoutManager mLayoutManager; 

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

    p = getIntent().getStringExtra("variant_id"); 
    i = getIntent().getStringExtra("product_id"); 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     p = extras.getString("variant_id"); 
     i= extras.getString("product_id"); 
    } 

    toolbar = (Toolbar) findViewById(R.id.customToolBar); 
    setSupportActionBar(toolbar); 
    setTitle("Check-out"); 
    toolbar.setTitleTextColor(Color.BLACK); 

    toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black); 
    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onBackPressed(); 
     } 
    }); 

    helper = new DatabaseHandler(this); 
    dbList= new ArrayList<ProductModel>(); 
    dbList = helper.getDataFromDB(); 

    mRecyclerView = (RecyclerView)findViewById(R.id.rv_cart_item_list); 

    // use a linear layout manager 
    mLayoutManager = new LinearLayoutManager(this); 
    mRecyclerView.setLayoutManager(mLayoutManager); 

    mAdapter = new CartAdapter(this,dbList); 
    mRecyclerView.setAdapter(mAdapter); 

    tvTotalNoOfItems = (TextView)findViewById(R.id.tvTotalCartItems); 
    tvTotalPrice = (TextView)findViewById(R.id.tvTotalCartItemsPrice); 
    String totalPrice = ""; 
    for (int i = 0; i<dbList.size(); i++) 
    { 
     totalPrice = totalPrice + dbList.get(i).getPrice().toString(); 
    } 
    tvTotalPrice.setText(totalPrice); 

    btnContinueShopping = (Button)findViewById(R.id.btnBackToProductActivity); 
    btnContinueShopping.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent launchCOllectionActivity = new Intent(CartActivity.this, CollectionActivity.class); 
      startActivity(launchCOllectionActivity); 
      finish(); 
     } 
    }); 
    btnCheckout = (Button)findViewById(R.id.btn_checkout); 
    btnCheckout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent launchCheckoutActivity = new Intent(CartActivity.this,CheckoutActivity.class); 
      startActivity(launchCheckoutActivity); 
     } 
    }); 


} 

}

+0

使用adapter.getItemCount()獲取總項目 –

回答

-1

只需添加適配器下面這條線設置線

tvTotalNoOfItems.setText(mAdapter.getCount()); 

,並添加到這一點你在哪裏,你刪除動作適配器類進行

String totalPrice = ""; 
((CartActivity)context).tvTotalNoOfItems.setText(getCount()); 
for (int i = 0; i<productModelList.size(); i++) 
{ 
    totalPrice = totalPrice + productModelList.get(i).getPrice().toString(); 
} 
((CartActivity)context).tvTotalPrice.setText(""+totalPrice); 

只是公共你的TextView這樣

public TextView tvTotalNoOfItems, tvTotalPrice; 
+0

這真的是一個糟糕的解決方案。你的adaper知道具體的活動,所以我不能用它與另一個 - 它會崩潰 –

+0

它不是一個壞的解決方案,但如果您的適配器的具體活動比它工作正常..! –

+0

它會工作,但最好有適配器,你可以重用。儘量避免強制轉換爲抽象類中的特定類,使用 –

1

首先,添加以下消氣劑適配器:

public List<ProductModel> getItems(){ 
     return productModelList; 
} 

然後,您可以訂閱適配器數據更改和請執行以下操作:

mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { 
     @Override 
     public void onChanged() { 
      tvTotalNoOfItems.setText(mAdapter.getItemCount()); 
      String totalPrice = ""; 
      for (int i = 0; i < ((CartAdapter)mAdapter).getItems().size(); i++) { 
       totalPrice = totalPrice + ((CartAdapter)mAdapter).getItems().get(i).getPrice().toString(); 
      } 
      tvTotalPrice.setText("" + totalPrice); 
     } 
    }); 
+0

並不安全,它無法解析mAdapter.getItems –

+0

我改變了,現在就嘗試。所有你需要的 - 將適配器上的活動轉換爲CartAdapter。或者您可以簡單地聲明: '私人CartAdapter mAdapter;' 而不是'RecyclerView.Adapter',即使沒有forcecast –