添加行tablelayout另一个函数

时间:2013-06-04 08:52:20

标签: java android mobile

我打算使用这个程序,当你按下按钮向表中添加一行时,但是它给了我错误,是否有人可以确定你正在做的任何错误?

这是主要的:

public class MainActivity extends Activity  {      
Button b1;
TableLayout tl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // setup the layout
    setContentView(R.layout.activity_main);

    // add a click-listener on the button
    b1 = (Button) findViewById(R.id.b1);

    tl = (TableLayout) findViewById(R.id.tLt);

    TableRow tr_head = new TableRow(this);
    tr_head.setBackgroundColor(Color.GRAY);
    tr_head.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

    TextView label_name = new TextView(this);
    label_name.setText("Nome");
    label_name.setTextColor(Color.WHITE);
    tr_head.addView(label_name);

    TextView label_surname = new TextView(this);
    label_surname.setText("Apelido"); 
    label_surname.setTextColor(Color.WHITE); 
    tr_head.addView(label_surname); 

    tl.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //botão
            EditText name = (EditText) findViewById(R.id.eTname);
            EditText subname = (EditText) findViewById(R.id.eTsubname);
            String nname = name.getText().toString();
            String sname = subname.getText().toString(); 

            adicionalinha adicionar = new adicionalinha();
            adicionar.adiciona(nname, sname, tl);       
        }
    });
}
}

这是adicionalinha的另一个功能:

public class adicionalinha extends Activity {

public void adiciona(String name, String subname, TableLayout tl){
    // Cria linha
    TableRow tr = new TableRow(this);

    tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

    TextView labelNAME = new TextView(this);
    labelNAME.setText(name);
    labelNAME.setTextColor(Color.WHITE);
    tr.addView(labelNAME);

    TextView labelAPELIDO = new TextView(this);
    labelAPELIDO.setText(subname);
    labelAPELIDO.setTextColor(Color.WHITE);
    tr.addView(labelAPELIDO);

    // adiciona linha
    tl.addView(tr);
}
}

0 个答案:

没有答案