Agregar, guardar y eliminar tablerow
Frecuentes
Visto 276 equipos
0
Quiero crear un table
con un button
that every time it is clicked adds a tablerow
con tres TextView
. For the moment I could only add to the tablerow
sin TextView
, however, has the problem that when I close the application the lines created are not saved, who can help me?
Este es mi codigo:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddItem = (Button) findViewById(R.id.addItemTableRow);
btnAddItem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
TableRow row = new TableRow(MainActivity.this);
TextView t = new TextView(MainActivity.this);
t.setText("Add table row");
row.addView(t);
table.addView(row, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
});
}
}
1 Respuestas
0
You can't add permanently views to a Layout ("permanently" means that survive to the application being closed).
If you are referring to the application being put in background (i.e. when the user presses the home button), or when the user turns the device, or when you start another activity and you go back to this one android gives you the possibility to "save" the state of your activity with two callbacks: namely onSaveInstanceState y onRestoreInstanceState
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("numRows", rowsNum);
for (int i=1;i<rowsNum+1;i++) {
savedInstanceState.putString("row"+i+"1", "Text of the first row first cell");
savedInstanceState.putString("row"+i+"2", "Text of the first row second cell");
savedInstanceState.putString("row"+i+"3", "Text of the first row third cell");
}
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
int numRows = savedInstanceState.getInt("numRows");
for (int i=1;i<rowsNum+1;i++) {
String cell1 = savedInstanceState.getString("row"+i+"1");
String cell2 = savedInstanceState.getString("row"+i+"2");
String cell3 = savedInstanceState.getString("row"+i+"3");
//re-create the table here from the strings
}
}
Espero que esto ayude
contestado el 28 de mayo de 14 a las 12:05
thanks for the help...I wanted to do by creating XML files but do not know how to do - user3668723
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android save row android-tablelayout tablerow or haz tu propia pregunta.
i add the tablerow but when i close the application the tablerow are not saved. - user3668723
if you want to save the data. you just try SqLite - Ramki Anba