在android studio java中动态添加行



我想通过点击按钮id-AddButton添加新行,我找到了很多谷歌源,但我无法做到。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/NameTextView"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:layout_marginLeft="20sp"
android:layout_marginTop="10sp"
android:text="Name"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold" />
<EditText
android:id="@+id/PersonName"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:layout_marginLeft="20sp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mobile Number"
android:layout_marginLeft="20sp"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold" />
<EditText
android:id="@+id/PersonPhone"
android:layout_width="325dp"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginLeft="20sp"
android:hint="Mobile number"
android:inputType="phone" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Address"
android:layout_marginLeft="20sp"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold" />
<EditText
android:id="@+id/PersonAddress"
android:layout_width="328dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:ems="10"
android:hint="Delivery Address"
android:inputType="textPostalAddress" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:text="                         Order Details"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TableLayout
android:id="@+id/MainTable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:stretchColumns="0,1,2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#000000"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text=" Date "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text="Item"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="2"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:text="Quantity"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/AddButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/DoneButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Done" />
</LinearLayout>
</ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

当点击add按钮时,我需要动态添加多行,我在java中使用button.setOnClickListener,但我一直在创建新行。如果有人可以的话,请帮帮我。还有一件事,我有另一个账户,它显示我已经达到了发布问题的限制,我能知道限制吗,为什么会显示,因为我只发布了大约4-5个问题。

在添加按钮的onClickListner中,以编程方式实现添加TableRow及其元素的代码。

JAVA部件

TableLayout tableLayout;
TableLayout tableLayout = (TableLayout) findViewById(R.id.MainTable);

为新行创建表格行标题

TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);    
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));

将数据添加到行我将向创建的TableRow添加两个TextView,您可以根据需要添加。

TextView label_hello = new TextView(this);
label_hello.setId(20); // define id that must be unique
label_hello.setText("HELLO"); // set the text  
label_hello.setTextColor(Color.WHITE);      
label_hello.setPadding(5, 5, 5, 5); 

TextView label_android = new TextView(this); 
label_android.setId(21);// define id that must be unique
label_android.setText("ANDROID..!!"); // set the textlabel_android.setTextColor(Color.WHITE); // set the color 
label_android.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_hello);// add the column to the table row 
tr_head.addView(label_android);

将列添加到表行之后,添加表行的时间是我们在开始时获取的主表布局

tableLayout.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,                 
LayoutParams.MATCH_CONTENT));      

建议
在这种情况下,建议将RecyclerView与自定义布局一起使用。如果存在大量数据,您正在实现的方法将导致严重的性能问题。此外,使用RecyclerView 可以更容易地添加/读取/删除数据

最新更新