Search code examples
androidandroid-linearlayout

How to edit a TextView which has been dynamically added?


I am currently developing a counter app. By pressing the "add" button a new row of certain elements are added. The text view along with 2 buttons are 3 of the elements. I have 2 buttons either side of the textView number, when i press the button on the left side it adds one to the TextView. This works fine But when i dynamically add a new row, when i press the button it changes the value of the first dynamically added row instead of the row i clicked the button on. How can it be so that it gets the row the button was clicked on and adds to that specific row. Here is the code:

MainActivity:

public class MainActivity extends AppCompatActivity {

    private LinearLayout parentLinearLayout;

    private int numtest = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
    }
    public void onAddField(View v) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.field, null);
        // Add the new row before the add field button.
        parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
    }

    public void addone(View v){
        numtest+=1;
        TextView t = (TextView) findViewById(R.id.counter);
        t.setText(numtest+"");
    }

    public void subtractone(View v){
        numtest-=1;
        TextView t = (TextView) findViewById(R.id.counter);
        t.setText(numtest+"");
    }

}

field.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:inputType="textWebEditText"/>
    <Spinner
        android:id="@+id/type_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:entries="@array/types"
        android:gravity="right" />
    <Button
        android:id="@+id/add_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="@android:drawable/ic_delete"
        android:onClick="addone"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:id="@+id/counter"
        android:textSize="30dp"
        android:layout_centerHorizontal="true"/>
    <Button
        android:id="@+id/subtract_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="@android:drawable/ic_delete"
        android:onClick="subtractone"/>

</LinearLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical" >
    <Button
        android:id="@+id/add_field_button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#555"
        android:layout_gravity="center"
        android:onClick="onAddField"
        android:textColor="#FFF"
        android:text="Add Field"
        android:paddingLeft="5dp"/>
</LinearLayout>

Thanks in advance


Solution

  • i think you should track at which index in your parent layout the button is clicked a simple approach is view tag

    public void onAddField(View v) {
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.field, null);
     // Add the new row before the add field button.
     //put index tag on your view
    rowView.findViewById(R.id.add_button).setTag(parentLinearLayout.getChildCount() - 1)
    rowView.findViewById(R.id.substract_button).setTag(parentLinearLayout.getChildCount() - 1)   
        parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
        }
    

    now u have that index to trace you can get respected view to update

    public void addone(View v){
            numtest+=1;
            View parent = parentLinearLayout.getChildAt(v.getTag());  
            TextView t = (TextView) parent.findViewById(R.id.counter);
            t.setText(numtest+"");
        } /// similarly for other method substract
    

    hope this work for you