2

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?

tanhatarin tanha
  • 93
  • 1
  • 1
  • 6
  • you need to show that you have attempted to work on this problem yourself first before just posting a question here....post your current work please – grepit Jun 10 '13 at 19:20

2 Answers2

12

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);
    }

}
}
Esteam
  • 1,911
  • 1
  • 16
  • 24
0

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

Valentin Grégoire
  • 1,110
  • 2
  • 12
  • 29
  • I dont have an object.i want to get data from user and how delete a arbitrary row from your code? – tanhatarin tanha Jun 10 '13 at 19:29
  • It doesn't matter where you get the data, this is just an example. You can put in literals in the setText parts (.setText("Testing...")). Removing a row would be done like this: `table.removeView(row);` Notice that it accepts a View object. You could also use the `RemoveViewAt(int index)`, but in my example it is useless. – Valentin Grégoire Jun 10 '13 at 19:37
  • thanks.Something that I want is similar to your code.but i want have a delete button in each row for delete it.(Without person object) – tanhatarin tanha Jun 10 '13 at 19:39
  • As edited above. If you want the extra button, you can do this: `Button deleteButton = new Button(this); row.addView(deleteButton);` For more info on how to handle the onClick event, check the answer from dicklaw795 on this thread: http://stackoverflow.com/questions/1851633/how-to-add-button-dynamically-in-android – Valentin Grégoire Jun 10 '13 at 19:44