how add and delete rows to table layout in java programically

十年热恋 提交于 2019-12-13 16:17:38

问题


I am new in java and android programing. I want to add rows(include some text box) to table layout by code and delete some of them.and finaly get their text box valus.how can i do it?


回答1:


Here is a simple example to do what you want:

Layout:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/table"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

</TableLayout>

Activity:

public class TableLayoutActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_layout);
    final TableLayout tableLayout = (TableLayout) findViewById(R.id.table);

    for (int i = 0; i < 5; i++) {
        // Creation row
        final TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

        // Creation textView
        final TextView text = new TextView(this);
        text.setText("Test" + i);
        text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

        // Creation  button
        final Button button = new Button(this);
        button.setText("Delete");
        button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TableRow parent = (TableRow) v.getParent();
                tableLayout.removeView(parent);
            }
        });

        tableRow.addView(text);
        tableRow.addView(button);

        tableLayout.addView(tableRow);
    }

}
}



回答2:


I recently got the same problem. I fixed it like this. Suppose your TableLayout is called table and als has this id in the xml layout.

<TableLayout
                android:id="@+id/table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

Suppose you have a list of Person objects, this is the way to populate the table:

ArrayList<Person> persons = getPersonList(); // --> suppose getting the list from this function
TableLayout table = (TableLayout) findViewById(R.id.table);
for(Person person : persons) {
    TableRow row = new TableRow(this);
    TextView tvName = new TextView(this);
    TextView tvAge = new TextView(this);
    TextView tvMail = new TextView(this);
    tvName.setText(person.getName());
    tvAge.setText(String.valueOf(person.getAge());
    tvMail.setText(person.getMail());
    row.addView(tvName);
    row.addView(tvAge);
    row.addView(tvMail);
    table.addView(row);
}

This basically means that each person has its own row. For every property of your person, 1 column is used.

Regards



来源:https://stackoverflow.com/questions/17031006/how-add-and-delete-rows-to-table-layout-in-java-programically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!