How to retrive all views data from Recyclerview Android at once

Viewed 715

I have a recyclerview in my android project and in the recyclerview im populating the name of the any items and it have EditText corresponding to every name view inside recyclerview, which means the person can edit the price of each item inside recyclerview. Now outside recyclerview i have a button which when clicked a POST request is called and the names and their corresponding prices are updated. I can't find a way by which i get all the data from recyclerview back to my fragment.

My sample code to understand the problem

public class TestsListAdapterLabsInside extends RecyclerView.Adapter<TestsListAdapterLabs.MyHolder> {

        Context context;
        ArrayList<TestListLabRetro> arrayList;
        double[] TOTAL;

        public TestsListAdapterLabsInside(Context context, ArrayList<TestListLabRetro> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
            this.TOTAL = new double[arrayList.size()];
        }

        @NonNull
        @Override
        public TestsListAdapterLabs.MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_test_labs2, viewGroup, false);

            return new TestsListAdapterLabs.MyHolder(v);
        }

        @Override
        public void onBindViewHolder(@NonNull app.dezignoo.knockadoc.Adapters.TestsListAdapterLabs.MyHolder myHolder, final int i) {

            myHolder.name.setText(arrayList.get(i).getName());
            myHolder.slno.setText(String.valueOf(i + 1));

            myHolder.price.setText(arrayList.get(i).getPrice());

        }

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


        public class MyHolder extends RecyclerView.ViewHolder {

            TextView slno, name;
            TextView price;

            public MyHolder(View itemView) {
                super(itemView);

                name = itemView.findViewById(R.id.testName);
                slno = itemView.findViewById(R.id.slno);
                price = itemView.findViewById(R.id.testPrice);
            }
        }

    }

this Adapter is defined below my fragment. I want a final list of all the <name, price> from the recyclerview in my fragment to update to the server.

Thankyou in advance.

2 Answers

Step 1) First you need to add a textChangeListner on your editText and update your arrayList in it

    myHolder.editText.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) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            TestListLabRetro testListLabRetro = arrayList.get(i);
            testListLabRetro.setPrice(editable.toString());
            arrayList.set(i, testListLabRetro);
        }
    });

Step 2) Create a getData Method

public ArrayList<TestListLabRetro> getData(){
    return this.arrayList;
}

Step 3) call getData method from your adapter where you are making a POST request to get updated arrayList

After your POST request, add the data to the ArrayList list and call adapter.notifydatasetchaged()

Related