简体   繁体   中英

Change the a particular textview in multiple textviews when it's respective button is clicked

I have multiple items and each item has a textview specifying the number of items selected by the user. When the user clicks on a button, the respective textview must be changed displaying updated number. Is there any other way than giving each item's button and textview separate ids and writing code for them ?

Here is the xml file that helps to understand I'm new to android programming and any advice or help would be a lot of help to me. Thanks.

    <TableLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">
    <TableRow>
        <LinearLayout android:layout_height="200dp" android:orientation="vertical"
            android:layout_width="wrap_content"
            android:background="@drawable/abc"/>
    </TableRow>
    <TableRow>
        <TextView android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:padding="5dp"
            android:text="ABC"/>
    </TableRow>
    <TableRow>
        <LinearLayout android:orientation="horizontal">
            <TextView
                android:id="@+id/itemsNum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Items : 0"/>
            <Button
                android:id="@+id/plusBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ADD"/>
        </LinearLayout>
    </TableRow>
    <TableRow>
        <LinearLayout android:layout_height="300dp" android:orientation="vertical"
            android:layout_width="wrap_content"
            android:background="@drawable/xyz"/>
    </TableRow>
    <TableRow>
        <TextView android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="XYZ"/>
    </TableRow>
    <TableRow>
        <LinearLayout android:orientation="horizontal">
            <TextView
                android:id="@id/itemsNum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Items : 0"/>
            <Button
                android:id="@id/plusBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ADD"/>
        </LinearLayout>
    </TableRow>
    <TableRow>
        <LinearLayout android:layout_height="300dp" android:orientation="vertical"
            android:layout_width="wrap_content"
            android:background="@drawable/ijk"/>
    </TableRow>
    <TableRow>
        <TextView android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="IJK"/>
    </TableRow>
    <TableRow>
        <LinearLayout android:orientation="horizontal">
            <TextView
                android:id="@id/itemsNum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Items : 0"/>
            <Button
                android:id="@id/plusBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ADD"/>
        </LinearLayout>
    </TableRow>
    <TableRow>
        <LinearLayout android:layout_height="300dp" android:orientation="vertical"
            android:layout_width="wrap_content"
            android:background="@drawable/abc1"/>
    </TableRow>
    <TableRow>
        <TextView android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="ABC1"/>
    </TableRow>
    <TableRow>
        <LinearLayout android:orientation="horizontal">
            <TextView
                android:id="@id/itemsNum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Items : 0"/>
            <Button
                android:id="@id/plusBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ADD"/>
        </LinearLayout>
    </TableRow>
</TableLayout>

If they are going to always be the same layout, you should create a custom widget that extends View that contains the TextView and Button . If you never need to reference them from code you wouldn't need to write anything after doing it once. If you do need to reference it from code, you are going to have to set an id on your custom view widget in order to reference it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM