Android. When a program is added programmatically, it doesn't show
I have a problem with my android app. I am trying to dynamically add a row to a TableLayout. Situation: When I click on a specific button, I want to add a row to the TableLayout, the text inside depends on what is being filled in some EditTexts.
This is part of my XML file:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:stretchColumns="0,1"
android:id="@+id/landenTable"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</TableLayout>
Here is my Rekenmodel.java file:
public class Rekenmodel extends Activity{
TableLayout landenTable;
Data context = Data.GetInstance();
String binnenUwRegio, selectedCountry, buitenUwRegio, naar085Bedrijfsnummers, naar088Bedrijfsnummers, naar0900Servicenummers, overigeGesprekken;
Button b_send, b_addCountry;
Spinner countries;
EditText et_landen_aantalGesprekken, et_landen_bedrag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kpn_rekenmodel);
landenTable = (TableLayout) findViewById(R.id.landenTable);
countries = (Spinner) findViewById(R.id.countries);
et_landen_aantalGesprekken = (EditText)this.findViewById(R.id.et_landen_aantalGesprekken);
et_landen_bedrag = (EditText)this.findViewById(R.id.et_landen_bedrag);
b_addCountry = (Button)this.findViewById(R.id.addCountryButton);
b_addCountry.setOnClickListener(addCountryButtonListener);
}
private OnClickListener addCountryButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
final TableRow tr = new TableRow(Rekenmodel.this);
TextView landKeuze = new TextView(Rekenmodel.this);
landKeuze.setText(countries.getSelectedItem().toString());
landKeuze.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.45f));
tr.addView(landKeuze);
Button delete = new Button(Rekenmodel.this);
delete.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.05f));
delete.setText("Delete");
delete.setTextSize(10f);
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
landenTable.removeView(tr);
}
});
tr.addView(delete);
TextView aantalGesprekken = new TextView(Rekenmodel.this);
aantalGesprekken.setText(et_landen_aantalGesprekken.getText().toString());
aantalGesprekken.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.15f));
tr.addView(aantalGesprekken);
TextView duur = new TextView(Rekenmodel.this);
duur.setText("Test duur");
duur.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.20f));
tr.addView(duur);
TextView bedrag = new TextView(Rekenmodel.this);
bedrag.setText(et_landen_bedrag.getText().toString());
bedrag.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.15f));
tr.addView(bedrag);
landenTable.addView(tr);
}
};
}
+3
source to share