简体   繁体   English

显示号码 android studio中textview中的recyclerview项目

[英]Display no. of items of a recyclerview in a textview in android studio

I want to display number of total items of my recyclerview in a textview. 我想在textview中显示我的recyclerview的总项目数。 If new item is added or deleted that textview should be update. 如果添加或删除了新项目,则应更新textview。 And also calculate total price of items in a list of Items in a recyclerview and display in a textview below recyclerview list. 并且还计算Recyclerview中项目列表中项目的总价格,并显示在recyclelerview列表下方的textview中。

Below is my recyclerview adapter: 以下是我的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;
        }
    }

and java Class: 和java类:

public class CartActivity extends 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);
            }
        });
    }
}

First, add following getter to your adapter: 首先,将以下getter添加到适配器:

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

Then, you can subscribe on adapter data change and do the following: 然后,您可以订阅适配器数据更改并执行以下操作:

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

just add this line below the adapter set line 只需在适配器设置行下面添加此行

tvTotalNoOfItems.setText(mAdapter.getCount());

and add this into you adapter class where you delete action perform 并将其添加到您删除操作执行的适配器类中

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

Just public you textview like this 只是公开你这样的textview

public TextView tvTotalNoOfItems, tvTotalPrice;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新android studio后RecyclerView不显示列表中的所有项目 - RecyclerView doesn't display all items in list after update android studio Android Studio RecyclerView 不显示项目 - Android Studio RecyclerView does not show items 在 RecyclerView Android Studio 中为 Arraylist 中的项目设置 ID - Setting ids to items in an Arraylist in a RecyclerView Android Studio Android在所有项目中的recyclerview中更改textview而不重绘整个列表 - Android change textview in recyclerview in all items without redrawing the entire list 允许在 android recyclerview 中一次从不同的 textview 项目中选择文本 - Allow selecting text from different textview items at once in android recyclerview android recyclerview 不显示项目 - android recyclerview doesn't display items 如何在 Android Studio 中使用 RecyclerView 显示数据? - how to display data using RecyclerView in Android Studio? 在recyclerview android studio中动态显示按钮 - Display buttons dynamically in recyclerview android studio 如何在android studio的textview中显示用户的Android Mac地址 - How to display the users Android Mac Address in a textview on android studio ANDROID-如何在android studio中将数据库mysql php的值显示为textview - ANDROID - How to display value of database mysql php into textview in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM