How to get data from Edit Text in a RecyclerView?
Asked Answered
P

4

13

I have recyclerview with edit text. Each row has a edit text. I am entering values in the edit text manually and after i enter values, I want to get those values in each and every row. I want to get those values to the main activity. There may be 10 to 20 rows in the recyclerview. I want to get all values in those edit texts.

This is my Adapter

public class SelectItemAdapter extends RecyclerView.Adapter<SelectItemAdapter.ItemHolder> {

    private List<String> itemsName, itemsQty, itemsPCode, itemPlant;
    private OnItemClickListener onItemClickListener;
    private LayoutInflater layoutInflater;

    public SelectItemAdapter(Context context, String[] mDataset) {
        layoutInflater = LayoutInflater.from(context);
        itemsName = new ArrayList<String>();
        itemsQty = new ArrayList<String>();
        itemsPCode = new ArrayList<String>();
        itemPlant = new ArrayList<String>();
        this.mDataset = mDataset;


    }


    @Override
    public SelectItemAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.custom_row_selected_item, parent, false);
        return new ItemHolder(itemView, this);
    }

    @Override
    public void onBindViewHolder(SelectItemAdapter.ItemHolder holder, final int position) {
        holder.setItemName(itemsName.get(position));
        holder.setItemQty(itemsQty.get(position));
        holder.setItemPCode(itemsPCode.get(position));
        holder.setItemPlant(itemPlant.get(position));

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }


    @Override
    public int getItemCount() {
        return itemsName.size();
    }

    public Object getItemName(int position) {
        return itemsName.get(position);
    }

    public Object getItemPCode(int position) {
        return itemsPCode.get(position);
    }

    public Object getItemPlant(int position) {
        return itemPlant.get(position);
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }


    public interface OnItemClickListener {
        public void onItemClick(ItemHolder item, int position);
    }

    public void add(int location, String iName, String iQty, String iPCode, String iPlant) {
        itemsName.add(location, iName);
        itemsQty.add(location, iQty);
        itemsPCode.add(location, iPCode);
        itemPlant.add(location, iPlant);
        notifyItemInserted(location);
    }

    public void remove(int location) {
        if (location >= itemsName.size())
            return;

        itemsName.remove(location);
        notifyItemRemoved(location);
    }

    public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private SelectItemAdapter parent;
        TextView textItemName, txtPCode, txtAvailableQty, txtTempQty, txtPlant;
        Button bRemove;
        EditText numPicker;

        public ItemHolder(View itemView, SelectItemAdapter parent) {
            super(itemView);
            this.parent = parent;
            textItemName = (TextView) itemView.findViewById(R.id.txtProductName);
            txtAvailableQty = (TextView) itemView.findViewById(R.id.txtAvailbleQty);
            txtPCode = (TextView) itemView.findViewById(R.id.txtPCode);
            txtPlant = (TextView) itemView.findViewById(R.id.txtPlant);
            bRemove = (Button) itemView.findViewById(R.id.bRemove);
            numPicker = (EditText) itemView.findViewById(R.id.numberPicker);
            bRemove.setOnClickListener(this);
        }

        public void setItemName(CharSequence name) {
            textItemName.setText(name);
        }

        public void setItemQty(CharSequence name) {
            txtAvailableQty.setText(name);
        }

        public void setItemPCode(CharSequence name) {
            txtPCode.setText(name);
        }

        public void setItemPlant(CharSequence name) {
            txtPlant.setText(name);
        }

        public String getQtyNumber() {
            return numPicker.getText().toString();
        }

        public CharSequence getItemName() {
            return textItemName.getText();
        }


        public CharSequence getItemPCode() {
            return txtPCode.getText();
        }

        @Override
        public void onClick(View v) {
            final OnItemClickListener listener = parent.getOnItemClickListener();
            if (listener != null) {
                listener.onItemClick(this, getPosition());
            }
        }
    }

}

This is how i tried to get values. But it is not working

private String getNumPicNumber(int i) {
        if (myRecyclerView.findViewHolderForLayoutPosition(i) instanceof SelectItemAdapter.ItemHolder) {
            SelectItemAdapter.ItemHolder childHolder = (SelectItemAdapter.ItemHolder) myRecyclerView.findViewHolderForLayoutPosition(i);
            numberPickerNumber = childHolder.getQtyNumber();
        }
        return numberPickerNumber;
    }
Peary answered 8/5, 2016 at 5:27 Comment(1)
How you handle the focus on Edit text in RecyclerView? Doesn't it lose focus on scroll.Foiled
D
18

You don't need to use so many lists, just create a class that will contain all the data of single item, there is no need for buttons, use just text change listener instead.

sample code

public class RetItem
{
    public String _itemName;
    public String _itemQty;
    public String _itemPcode;
    public String _itemPlant;
}

public class SelectItemAdapter extends RecyclerView.Adapter<SelectItemAdapter.ItemHolder> {

    private List<RetItem> _retData;
     public SelectItemAdapter(Context context, String[] mDataset) {
        layoutInflater = LayoutInflater.from(context);
        _retData = new ArrayList<RetItem>(mDataset.length);
        this.mDataset = mDataset;
    }

      @Override
    public void onBindViewHolder(SelectItemAdapter.ItemHolder holder, final int position) {
        holder.setItemName(itemsName.get(position));
        holder.setItemName.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {}

           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start, int before, int count) {
             _retData.get(position)._itemName = s.toString();
           }
          });

        holder.setItemQty(itemsQty.get(position));
        holder.setItemQty.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {}

           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start, int before, int count) {
             _retData.get(position)._itemQty = s.toString();
           }
          });

        holder.setItemPCode(itemsPCode.get(position));
        holder.setItemPCode.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {}

           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start, int before, int count) {
             _retData.get(position)._itemPcode = s.toString();
           }
          });       
        holder.setItemPlant(itemPlant.get(position));
        holder.setItemPlant.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {}

           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start, int before, int count) {
             _retData.get(position)._itemPlant = s.toString();
           }
          });
    }

    public List<RetItem> retrieveData()
    {
        return _retData;
    }
}
Dillard answered 8/5, 2016 at 7:6 Comment(5)
while using addTextChangedListener on each of the editTexts, the text will be in s CharSequence, this text will be saved in _retData structure as you can see. when you want to get the data from the adapter just call retrieveData functionDillard
no they are not edit text. Those you put addTextChangedListener, they are text view. **numPicker ** is the edit text.Peary
so instead of holder.setItemQty.addTextChangedListener just do holder.numPicker.addTextChangedListenerDillard
thank you for the response. this helped me to go for a solution. so i accept this answerPeary
Thank you ,but for this to work we have to make few changes . _retData = new ArrayList<RetItem>(mDataset.length); // this doesnt initialse the retData array with an initial size , It only sets a maximum size for the array. An this will give an array index out of bounds exception So this is what we have to do _retData = new ArrayList<RetItem>(mData.size()); for (int i =0;i<mData.size();++i){ _retData.add(new RetItem("","")); }Fulford
G
2

To do this there are two ways:-

1) Add save Button in each row of RecyclerView on this Button click (onClick)

 @Override
        public void onClick(View v) {
           String ans = holher.numPicker.getText().toString();
           // save ans to sharedpreferences or Database
        }

2) Add onTextChangedListener to EditText

holher.mumPicker.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      String ans = holher.numPicker.getText().toString();
           // save ans to sharedpreferences or Database
   }
  });

Then in your MainActivity.java retrive data from sharedpreferences or database

Gould answered 8/5, 2016 at 6:12 Comment(0)
N
0

I had a similar problem.My Recyclerview contained one Textview, two EditTexts and one remove Button to remove the item from the Recyclerview. I am getting the data from both the Edittexts, from a model class using a button from my activity. Everything works well I just want to know what all optimizations I can make. Please give your suggestions in a detailed manner.

My Model Class:

public class OrderGS {
  String names, rty,qty;

    public OrderGS(String name, String rty, String qty) {
        this.names = name;
        this.rty = rty;
        this.qty = qty;
    }

    public String getName() {
        return names;
    }

    public void setName(String names) {
        this.names = names;
    }

    public String getRty() {
        return rty;
    }

    public void setRty(String rty) {
        this.rty = rty;
    }

    public String getQty() {
        return qty;
    }

    public void setQty(String qty) {
        this.qty = qty;
    }
}

Above class holds the three Strings one is the titile and the two hold the edittext data.

My Adapter code:

class OOHolderI extends RecyclerView.ViewHolder{

        TextView title;
        EditText rty,qty;
        public OOHolderI(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.text_order);
            rty = itemView.findViewById(R.id.editRTY);
            qty = itemView.findViewById(R.id.editQTY);
            itemView.findViewById(R.id.button_remove).setOnClickListener(v -> {
               arrayList.remove(getAdapterPosition());
               if(rtyArray.size()>0) {
                   rtyArray.remove(getAdapterPosition());
                   qtyArray.remove(getAdapterPosition());
               }
               notifyDataSetChanged();
           });
            rty.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                arrayList.get(getAdapterPosition()).setRty(s.toString());
                if(!rtyArray.contains(getAdapterPosition())){
                rtyArray.add(getAdapterPosition());}
                }
            });
            qty.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    arrayList.get(getAdapterPosition()).setQty(s.toString());
                    if(!qtyArray.contains(getAdapterPosition())) {
                        qtyArray.add(getAdapterPosition());
                    }
                }

            });


        }

}

In the adapter I am using two arraylists rtyArray and qtyArray to hold the position of the Edittext data in the model class.

public ArrayList<Integer> getRTY() {
    return rtyArray;
}
public ArrayList<Integer> getQTY(){
    return qtyArray;
} 

The above two functions return the positions of the Edittextdata

My Activity:

 findViewById(R.id.button_save).setOnClickListener(v -> {
            ArrayList<Integer> rtyArray = adapter.getRTY();
            ArrayList<Integer> qtyArray = adapter.getQTY();
            if(rtyArray.size()>0) {
               for(Integer i: rtyArray){
              //get data here like 
              arrayListOfModel.get(i).getRty(); 
              arrayListOfModel.get(qtyArray.get(i)).getQty();
               }
            }
        });
        adapter.notifyDataSetChanged();

In my case both the position holding arrays are always equal in size.

Narcolepsy answered 23/5, 2018 at 8:40 Comment(0)
T
0

To get text of clicked TextField in RecyclerView, in ViewHolder you can get text of clicked textfield.

   public class MyHolder extends RecyclerView.ViewHolder {
    private TextView myData;
    public MyHolder(View itemView) {
        super(itemView);
        myData = itemView.findViewById(R.id.txtTitle);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Clicked " + myData.getText(), Toast.LENGTH_LONG).show();
                
            }
        });
    }
}
Turnheim answered 10/10, 2023 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.