简体   繁体   中英

Getting edittext values from recycler view

I want to get values from the list of edit text from the Recycler view.

在此处输入图像描述 This is the layout

This is the code I am using in Activity class to get values from recycler view. Code consists of Error message in comments


private void getPriceQuantity() {
        List<PriceQuantity> priceQuantitiesToAdd = new ArrayList<PriceQuantity>();
        for(int i=0;i<priceQuantities.size();i++)
        {
            View view=priceQuantityRecyclerView.getChildAt(i);
            if(view!=null)
            {

//  this is the error i am receiving
//  java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable 
//  android.widget.EditText.getText()' on a null object reference

                EditText price = view.findViewById(R.id.product_price);
                Log.d(TAG," price is "+price.getText().toString());
                EditText quantity = view.findViewById(R.id.product_quantity);
            }
        }
    }

This is my view holder in Adapter class:

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            productPrice = itemView.findViewById(R.id.product_price);
            productQuantity = itemView.findViewById(R.id.product_quantity);
            deletePriceQuantity = itemView.findViewById(R.id.deletePriceQuantityButton);
            linearLayout = itemView.findViewById(R.id.priceQuantity);

            deletePriceQuantity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(priceQuantities.size() >1){
                        priceQuantities.remove(getAdapterPosition());
                        notifyItemRemoved(getAdapterPosition());
                    }
                }
            });
        }

This is onBindView Holder from Adapter class


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        addImageView.setOnClickListener(v -> {
            if(priceQuantities.size() <=2 ){
                Log.d("clicked","Add image is clicked");
                priceQuantities.add(new PriceQuantity());
                Log.d("clicked","size of array list "+priceQuantities.size());
                notifyDataSetChanged();
            }
        });

Please remove this code:

        Log.d(TAG," price is "+price.getText().toString());

try this:

RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) priceQuantityRecyclerView.findViewHolderForAdapterPosition(i);
if (null != holder) {
   EditText price = (EditText) holder.itemView.findViewById(R.id.product_price);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM