-1

In my app,I have number of buttons in Mainactivity and my app uses a recycler view to show the list items(row_layout).The list items(row_layout) consists of 2 edit text and one text view. I've created a custom adapter to display the row_layout.

Now, I want my app to update the two EditText and TextView on adapter based on button clicks made on the MainActivity but the problem is buttons are on MainActivity and the EditText and TextView are in adapter.So how do I achieve this?

Please don't mark this question as duplicate, I've searched a lot but I failed and this is my last chance to ask question in stackoverflow, Please help me

Thanks in advance.

This is my activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginBottom="13dp">


        <Button
            android:id="@+id/buttonl7"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="9dp"
            android:layout_marginBottom="4dp"
            android:padding="4dp"
            android:text="7" />

        <Button
            android:id="@+id/buttonr9"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="330dp"
            android:layout_marginBottom="0dp"
            android:padding="4dp"
            android:text="9" />

        <Button
            android:id="@+id/buttonr8"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="274dp"
            android:layout_marginBottom="2dp"
            android:padding="4dp"
            android:text="8" />

        <Button
            android:id="@+id/buttonr7"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="211dp"
            android:layout_marginBottom="4dp"
            android:padding="4dp"
            android:text="7" />

        <Button
            android:id="@+id/buttonl9"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="131dp"
            android:layout_marginBottom="5dp"
            android:padding="4dp"
            android:text="9" />

        <Button
            android:id="@+id/buttonl8"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="69dp"
            android:layout_marginBottom="7dp"
            android:padding="4dp"
            android:text="8" />


    </RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginBottom="161dp">

        <Button
            android:id="@+id/buttonl1"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="9dp"
            android:layout_marginBottom="4dp"
            android:padding="4dp"
            android:text="1" />

        <Button
            android:id="@+id/buttonr3"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="327dp"
            android:layout_marginBottom="2dp"
            android:padding="4dp"
            android:text="3" />

        <Button
            android:id="@+id/buttonr2"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="269dp"
            android:layout_marginBottom="2dp"
            android:padding="4dp"
            android:text="2" />

        <Button
            android:id="@+id/buttonr1"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="209dp"
            android:layout_marginBottom="5dp"
            android:padding="4dp"
            android:text="1" />

        <Button
            android:id="@+id/buttonl3"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="129dp"
            android:layout_marginBottom="6dp"
            android:padding="4dp"
            android:text="3" />

        <Button
            android:id="@+id/buttonl2"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="69dp"
            android:layout_marginBottom="7dp"
            android:padding="4dp"
            android:text="2" />


    </RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginBottom="87dp">


        <Button
            android:id="@+id/buttonl4"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="9dp"
            android:layout_marginBottom="4dp"
            android:padding="4dp"
            android:text="4" />

        <Button
            android:id="@+id/buttonr6"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="330dp"
            android:layout_marginBottom="3dp"
            android:padding="4dp"
            android:text="6" />

        <Button
            android:id="@+id/buttonr5"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="268dp"
            android:layout_marginBottom="3dp"
            android:padding="4dp"
            android:text="5" />

        <Button
            android:id="@+id/buttonr4"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="209dp"
            android:layout_marginBottom="6dp"
            android:padding="4dp"
            android:text="4" />

        <Button
            android:id="@+id/buttonl6"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="131dp"
            android:layout_marginBottom="6dp"
            android:padding="4dp"
            android:text="6" />

        <Button
            android:id="@+id/buttonl5"
            android:layout_width="54dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="69dp"
            android:layout_marginBottom="6dp"
            android:padding="4dp"
            android:text="5" />


    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="376dp"
        android:layout_height="262dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="5dp" />


</RelativeLayout>

This is my Main activity .java

package com.example.manuj.autocalc2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    Button buttonl0, buttonl1, buttonl2, buttonl3, buttonl4, buttonl5, buttonl6,
            buttonl7, buttonl8, buttonl9, buttonl10;
    Button buttonr0, buttonr1, buttonr2, buttonr3, buttonr4, buttonr5, buttonr6, buttonr7,
            buttonr8, buttonr9, buttonr10;

    RecyclerView recyclerView;
    CustomAdapter customAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//            button0 = (Button) findViewById(R.id.buttonl0);
        buttonl1 = (Button) findViewById(R.id.buttonl1);
        buttonl2 = (Button) findViewById(R.id.buttonl2);
        buttonl3 = (Button) findViewById(R.id.buttonl3);
        buttonl4 = (Button) findViewById(R.id.buttonl4);
        buttonl5 = (Button) findViewById(R.id.buttonl5);
        buttonl6 = (Button) findViewById(R.id.buttonl6);
        buttonl7 = (Button) findViewById(R.id.buttonl7);
        buttonl8 = (Button) findViewById(R.id.buttonl8);
        buttonl9 = (Button) findViewById(R.id.buttonl9);

        buttonr1 = (Button) findViewById(R.id.buttonr1);
        buttonr2 = (Button) findViewById(R.id.buttonr2);
        buttonr3 = (Button) findViewById(R.id.buttonr3);
        buttonr4 = (Button) findViewById(R.id.buttonr4);
        buttonr5 = (Button) findViewById(R.id.buttonr5);
        buttonr6 = (Button) findViewById(R.id.buttonr6);
        buttonr7 = (Button) findViewById(R.id.buttonr7);
        buttonr8 = (Button) findViewById(R.id.buttonr8);
        buttonr9 = (Button) findViewById(R.id.buttonr9);


        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        final ArrayList<Calc> arrayList = new ArrayList<>();
        customAdapter = new CustomAdapter(this, arrayList);


        Calc calc = new Calc("-", "-", "-");
        arrayList.add(calc);
        recyclerView.setAdapter(customAdapter);


        buttonl1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //when this is clicked, i want to update 1 to editText.
            }
        });
    }

}

This is my list_items(row_layout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <EditText
            android:id="@+id/itemEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="Enter The Item"
            android:text="" />

        <EditText
            android:id="@+id/amtEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="Enter The Amt"
            android:text="" />

        <TextView
            android:id="@+id/Price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:text="Price"
            android:textSize="18sp"/>

    </LinearLayout>
</LinearLayout>

This is my custom adapter class

package com.example.manuj.autocalc2;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.manuj.autocalc2.Calc;
import com.example.manuj.autocalc2.MainActivity;
import com.example.manuj.autocalc2.R;

import java.util.ArrayList;


public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

    private LayoutInflater inflater;
    public static ArrayList<Calc> editModelArrayList;


    public CustomAdapter(Context ctx, ArrayList<Calc> editModelArrayList){

        inflater = LayoutInflater.from(ctx);
        this.editModelArrayList = editModelArrayList;
    }

    @Override
    public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.list_items, parent, false);
        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, final int position) {


        holder.editText1.setText(editModelArrayList.get(position).getmItemEdit());
        holder.editText2.setText(editModelArrayList.get(position).getmAmtEdit());
        holder.textView.setText(editModelArrayList.get(position).getmAmtEdit());

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{

        protected EditText editText1;
        protected EditText editText2;
        protected TextView textView;

        public void updateItemEdit(String text){
            editText1.setText(editText1.getText());
        }

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


            editText1 = (EditText) itemView.findViewById(R.id.itemEdit);
            editText2=itemView.findViewById(R.id.amtEdit);
            textView=itemView.findViewById(R.id.Price);


            editText1.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) {

                    editModelArrayList.get(getAdapterPosition()).setmItemEdit(editText1.getText()+editText2.getText().toString());
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

            editText2.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) {

                    editModelArrayList.get(getAdapterPosition()).setmItemEdit(editText1.getText()+editText2.getText().toString());
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

        }

    }
}
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
  • Can you give clear detail explanation using any example what you want to achieve ? – Sandeep Insan Nov 24 '18 at 14:44
  • I have a 2 editText and one textView ,itemEdit,amtEdit,textTotal respectievely.These three are declared in list_items(custom Adapter) and i have some buttons like 1,2,3....So now whenever i press a button it should update the editText which is in list_items(RecyclerView).How do i access the recycler view elements in onClick method in MainActivity. – Manuj Sharma Nov 24 '18 at 15:22
  • You want to update editText value of all the items in list. Like you have a list of five items and if you pressing button 3 so you want to set editText value to 3 for all the five items of list ? – Sandeep Insan Nov 24 '18 at 16:38
  • not for all the five,but only for particular position. – Manuj Sharma Nov 24 '18 at 16:42
  • Add some UI of what you want to achieve. – Sandeep Insan Nov 24 '18 at 16:58
  • I have less reputation,i cant share photos.I'll explain again. – Manuj Sharma Nov 24 '18 at 17:06
  • I basically alow user to update the itemEdit when 1-9 number button is pressed,but i'm unable to do it as it the button is present in mainActivity and itemEdit is in adapter i.e in list_items.So my question is how to do it? – Manuj Sharma Nov 24 '18 at 17:10
  • You did not explained it clearly here want you want exactly. Up to i understood you wan to update the list with new data as the data changed ? – Sandeep Insan Nov 25 '18 at 02:40
  • i basically want to know how to communicate with activity and adapter – Manuj Sharma Nov 25 '18 at 18:59

2 Answers2

1

If I understand your problem correctly,you want update the recyclerview item.So,you can use this methods:

  • You can change your arraylist and call adapter.notifyDataSetChange
  • You can find child item of recyclerview and you can change it. There is sample
kamilyrb
  • 2,502
  • 3
  • 10
  • 25
  • My problem is that whenever i press a button (which is in mainActivity) it should update the editText (which is in list_items of adapter).This would have been easy if the button was in adapter,but in this case its in mainactivity,so i could nont set the edit text value directly... – Manuj Sharma Nov 24 '18 at 13:37
  • Ok,in your button onclick method, access recyclerview child item and change it.Isn't it possible? – kamilyrb Nov 24 '18 at 15:07
  • Okay,how do i achieve that. – Manuj Sharma Nov 24 '18 at 15:17
  • one of the methods is saving the position of the child and calling notifiyDataSetChanged() and in the onBindViewHolder if(position == savedPosition) do what you have to do – Master Fathi Nov 24 '18 at 15:44
  • View v = reyclerView.findViewHolderforAdaperPosition(position) Edittext et = v.findViewById(R.id.edittextId).You can try this. – kamilyrb Nov 24 '18 at 15:47
0

in activity

 Calc calc = new Calc("-", "-", "-");
    arrayList.add(calc);
    arrayList.add(calc);
    arrayList.add(calc);
    arrayList.add(calc);
    arrayList.add(calc);
    arrayList.add(calc);
    arrayList.add(calc);
    arrayList.add(calc);
    arrayList.add(calc);
    recyclerView.setAdapter(customAdapter);
//  add 9 values to list so that each button will change it's respective position

    buttonl1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Calc calc = new Calc("buttonl1", "buttonl1", "buttonl1");
                    customAdapter.updateEntryAt(0,calc);
                }
            });

     buttonl2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Calc calc = new Calc("buttonl2", "buttonl2", "buttonl2");
                    customAdapter.updateEntryAt(1,calc);
                }
            });

in adapter

public void updateEntryAt(int positionToUpdate,Calc calc){
     editModelArrayList.remove(positionToUpdate);
     editModelArrayList.add(positionToUpdate,calc);
     notifyDataSetChanged();
}

the method updateEntryAt will change the list inside your adapter remove the old value in the list that you started with and replace it with the new value passed to that method. and notifyDataSetChanged(); will refresh the adapter so it shows the new values.

Master Fathi
  • 349
  • 1
  • 9
  • 19
  • what is the difference between your current answer and previous answer.please explain me – Manuj Sharma Nov 24 '18 at 16:40
  • the previous answer was to change one value this one is to change the whole entity so 3 values at the same time in a more decent way. I looked to your question and tried to understand what u want to achieve and i came up with this – Master Fathi Nov 24 '18 at 17:34
  • ah if u meant the one just before this one i did a mistake it was editModelArrayList.add(positionToUpdate); and it must be editModelArrayList.add(positionToUpdate,calc); to add the object into the position required – Master Fathi Nov 24 '18 at 17:36
  • you need to understand what i'm trying to do you are updating the adapter at one position given to the adapter and changing it by the new calc(1,2,3) that i just created that's what i would do and it works for me. if u want give me more details about what's happening i could maybe help you. – Master Fathi Nov 25 '18 at 18:44
  • okay,thanks for your reply.I'll try once again and would tell you. – Manuj Sharma Nov 25 '18 at 18:56
  • did you find a solution? or did my solution work for you? if yes can you please mark this answer as the correct one? thank you :) – Master Fathi Nov 27 '18 at 22:54
  • Oh sorry,It didn't work or maybe i didn't understood your answer.It would be a great help if you could give the answer in more detailed. – Manuj Sharma Nov 29 '18 at 11:26
  • i have edit the answer and tried to make it more clear i dont see what dosent work for you it should work. if it didin't just edit your question and put there more details or tell me right here what the result looks like – Master Fathi Nov 29 '18 at 16:48
  • Thanks for the answer ,it was bit helpful but still i didn't achieve what i wanted.So leaving it.Thanks again. – Manuj Sharma Dec 03 '18 at 05:17