我的Android應用程序出現問題。我嘗試動態添加一行到TableLayout。情況: 當我點擊一個特定的按鈕時,我想要將一行添加到TableLayout中,在某些EditTexts中,文本內部取決於填充內容。Android - 當以編程方式添加的行不顯示
這是我的XML文件的一部分:
<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>
這裏是我的Rekenmodel.java文件:
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);
}
};
}
logcat中的任何相關? –
landenTable.invalidate()? – Simon
我通過應用此修復程序解決了此問題。 http://stackoverflow.com/questions/11963465/android-layoutparams-for-textview-makes-the-view-disappear-programatically – user2029070