How to Get Data from Edit Text in a Recyclerview

How to get data from Edit Text in a RecyclerView?

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

How get value from Edit Text in a RecyclerView Android

Make a listener class and define a function in it with one String param.
Add that listener in your recyclerView adapter constructor.
Now add 'addTextChangedListener' for your edittext in your viewHiolder class like this:

    et_Cantidad.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}

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

add that function you just defined in the listener in the 'onTextChanged' method. The onTextChanged will retrieve the text from the edittext when you're typing something and pass that text into the listener function.

Now, in the myActivity you'll get the listener function in the adapter class object and you'll get the string from the edittext as you type and then do whatever you want with that text.

Android Studio - How to get data from an EditText in a RecyclerView and put them into an ArrayList in the main activity?

All you have to do is to implement a custom callback to your activity with the index in which the edittext is edited and what is the new text.

Create a callback interface inside adapter

    public interface TextChangeCallback {
void textChangedAt(int position, String text);
}

Pass the inteface implementation to Adapter via constructor

public MyAdapter(Context ct, List players,TextChangeCallback callback){
context = ct;
playerList = (ArrayList) players;
this.callback = callback;
}

Trigger callback to activity whenever you edit the text using TextWatcher

holder.editText.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) {
if (s.equals("")){
return;
}
data.set(position, String.valueOf(s));
callback.textChangedAt(position, String.valueOf(s));

}

@Override
public void afterTextChanged(Editable s) {

}
});

Add this interface implementation to your adapter

    MyAdapter myAdapter = new MyAdapter(this, playerList, new MyAdapter.TextChangeCallback() {
@Override
public void textChangedAt(int position, String text) {
playerList.set(position, text);
Log.d("UPDATED LIST : ", String.valueOf(data));
}
});
recyclerView.setAdapter(myAdapter);

This will now update the ArrayList in adapter and activity as you edit the edittext from recyclerview.

You can find the complete implementation from this link

How to get data from all RecyclerView EditTexts

My solution is the same as the one posted by @MarcosVasconcelos. Here's some code:

In DataViewHolder class:

public void bindData(final EntryData entryData) {
lengthTextView.setText(entryData.getLength());
diameterTextView.setText(entryData.getDiameter());

lengthTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
entryData.setLength(charSequence.toString());
}

@Override
public void afterTextChanged(Editable editable) {

}
});

diameterTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
entryData.setDiameter(charSequence.toString());
}

@Override
public void afterTextChanged(Editable editable) {

}
});
}

As you can see, all you have to do is set a TextWatcher on each of the EditTexts. In the onTextChanged method, you assign the EditText's current text to the correct EntryData.

In DataAdapter class:

public List<EntryData> getData() {
return datas;
}

Getter method for the datas List.

And finally, all you have to do is add a Button to the MainActivity and set an OnClickListener on it that gets the datas List from the adapter and then gets the necessary values from each EntryData object, like so:

private void setupViews() {
// Your other code

Button listDataButton = (Button) findViewById(R.id.btn_listData);

listDataButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<EntryData> data = adapter.getData();

StringBuilder builder = new StringBuilder();

for (int i = 0; i < data.size(); i++) {
EntryData entryData = data.get(i);

builder.append(i)
.append("\n")
.append("Diameter: ")
.append(entryData.getDiameter())
.append("\n")
.append("Length: ")
.append(entryData.getLength())
.append("\n");
}

Log.i("MainActivity", builder.toString());
}
});

Edit: Brief explanation of StringBuilder:

The StringBuilder class is used to improve perfomance when concantenating large numbers of Strings.
You use it as follows:

  1. Initialization: StringBuilder builder = new StringBuilder();. You can also include an intial String or CharSequence in the constructor, or specify the capacity.
  2. Concatenation: You can insert new items to the builder by using builder.append(). This method accepts not only Strings, but also other data types such as int, char, and so on.
  3. Convert it to a String by using builder.toString().

Refer to this tutorial and the official documentation for more information.

How to get a string value from an EditText in RecyclerView?

Create a HashMap with Item position as key

HashMap<Integer, String> map = new HashMap<>;

Change the entry inside your

@Override
public void afterTextChanged(Editable s) {
String value = map.get(position); // here position is the key
if (value != null) {
map.put(position, holder.mPlayerName.getText().toString());
} else {
// Key might be present...
if (map.containsKey(key)) {
// Okay, there's a key but the value is null
} else {
// Definitely no such key
}
}
}

then get HashMap values using get() method from HashMap.

Android - How To retrieve EditText value from RecyclerView in MainActivity

Got it working, here is the edited code:

mAdapter = new MyClassAdapter(this, mDataset.size);
mRecyclerView.setAdapter(mAdapter);
mRecyclerview.setItemViewCacheSize(mDataset.size());

List<ContentValues> list = new ArrayList<>();

for (int i = 0; i < mDataset.size(); i++) {
View view = recyclerView.getChildAt(i);
EditText nameEditText = (EditText) view.findViewById(R.id.et_name);
String name = nameEditText.getText().toString();

ContentValues cv = new ContentValues();
cv.put(MyContract.MyEntry.COLUMN_NAME, name);
list.add(cv)
}

// I encapsulated this in a try catch
for (ContentValues c:list) {
mDb.insert(MyClassContract.MyClassEntry.TABLE_NAME, null, c);
}

How to get values from editText and other fields from recyclerview adapter for a submit

solved by following this tutorial

https://demonuts.com/android-recyclerview-with-edittext/

and when modified for my code, I get the values by doing this and accesing at the push of a button:

public void getEditValue(){

for(int i=0;i<saleDetailList.size();i++)
{
View view=rcvMoneyDevolution.getChildAt(i); // This will give you entire row(child) from RecyclerView
if(view!=null)
{
TextView textView= view.findViewById(R.id.txtSVSaleDetailsAdapter);
Log.e("Tag",textView.getText().toString());
Spinner mySpinner = view.findViewById(R.id.spnQuantityProducttoDevolution);
String text = mySpinner.getSelectedItem().toString();
Log.e("Tag",text);
String moneyDevolution = MoneyDevolutionAdapter.editModelArrayList.get(i).getEditTextValue();
Log.e("Tag",moneyDevolution);

}
}

}


Related Topics



Leave a reply



Submit