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?
2 Answers
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);
}
}
}
1 Comment
tanhatarin tanha
Thank you.how can i get EditTexts in all rows?(With the assumption that some of these tableRows have been removed)
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
4 Comments
tanhatarin tanha
I dont have an object.i want to get data from user and how delete a arbitrary row from your code?
Valentin Grégoire
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.tanhatarin tanha
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)
Valentin Grégoire
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: stackoverflow.com/questions/1851633/…