0

I have a RecyclerView and a Submit button on main xml page. The Recycler view has editable text field in it & may be 5 to 6 list will come from server. After inputting data into each recycler view item I will click on submit.

Now is it possible to read the recycler view content as a json object on click of submit button. I have no idea on this, so there is no work done on this. kindly give some suggestion.

Tek Nath Acharya
  • 1,676
  • 2
  • 20
  • 35
Vikas Acharya
  • 3,550
  • 4
  • 19
  • 52

2 Answers2

0

RecyclerView data is not some Magic data is just a list of objects passed to the adapter. Since all the objects are passed by reference you can edit it and save the value in the same list of object you were using to populate the data. And when calling the submit button you can convert this list of object to JSONObject or String using Gson.

Inside the Adapter

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

private String[] mDataset;

public MyAdapter(String[] myDataset) {
    mDataset = myDataset;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_edittext, parent, false);
    // pass MyCustomEditTextListener to viewholder in onCreateViewHolder
    // so that we don't have to do this expensive allocation in onBindViewHolder
    ViewHolder vh = new ViewHolder(v, new MyCustomEditTextListener());

    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    // update MyCustomEditTextListener every time we bind a new item
    // so that it knows what item in mDataset to update
    holder.myCustomEditTextListener.updatePosition(holder.getAdapterPosition());
    holder.mEditText.setText(mDataset[holder.getAdapterPosition()]);
}

@Override
public int getItemCount() {
    return mDataset.length;
}


public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public EditText mEditText;
    public MyCustomEditTextListener myCustomEditTextListener;

    public ViewHolder(View v, MyCustomEditTextListener myCustomEditTextListener) {
        super(v);

        this.mEditText = (EditText) v.findViewById(R.id.editText);
        this.myCustomEditTextListener = myCustomEditTextListener;
        this.mEditText.addTextChangedListener(myCustomEditTextListener);
    }
}

// we make TextWatcher to be aware of the position it currently works with
// this way, once a new item is attached in onBindViewHolder, it will
// update current position MyCustomEditTextListener, reference to which is kept by ViewHolder
private class MyCustomEditTextListener implements TextWatcher {
    private int position;

    public void updatePosition(int position) {
        this.position = position;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        // no op
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        mDataset[position] = charSequence.toString();
    }

    @Override
    public void afterTextChanged(Editable editable) {
        // no op
    }
}
}

The TextWatcher will update the String at the correct position of the Adapter. I think a better solution would be to use two way data binding. There could still be some bug in this example and if you could find it, will be great learning for you.

Source: https://stackoverflow.com/a/31860393/5933012

NIKHIL MAURYA
  • 289
  • 1
  • 15
0

Inside your adapter:

public ArrayList<String> getRecyclerData() {
   ArrayList<String> list = new ArrayList<>();
   for (String item : adapterList) {
        list.add(item)
   }
   return list;
}

adapterList - the list where you store data (from EditTexts)

Now:

submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yourNewList = adapter.getRecyclerData();
            }
});

Then you parse your list to JSON (JSON Array) and thats it.

SkypeDogg
  • 1,000
  • 2
  • 13
  • 28
  • this is what i'm currently doing, but I'm asking something like in web technology. can i read the complete object from recyclerView by its Id as json object. or can i observe data change in recyclerview by data binding. – Vikas Acharya Dec 11 '19 at 13:51
  • and also how to read modified adapterList, can you give me a snippet of that. as currently I'm modifying the source data and reading it on button click. i think your way is some what better than me. – Vikas Acharya Dec 11 '19 at 13:53