public class main extends Activity implements onclickListener {
/** Called when the activity is first created. */
//initialize a button and a counter
Button BtnOK;
Button BtnCalc;
int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setup the layout
setContentView(R.layout.main);
// add a click-listener on the button
BtnOK = (Button) findViewById(R.id.btnOK);
BtnOK.setonclickListener(new View.onclickListener() {
@Override
public void onclick(View v) {
// TODO Auto-generated method stub
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
table.removeViews(0, counter);
EditText etxtUser = (EditText)findViewById(R.id.etxtNo);
counter = Integer.parseInt(etxtUser.getText().toString());
for(int i=0;i<counter;i++) {
TableRow row = new TableRow(this);
TextView t = new TextView(this);
t.setText("Network " + (i+1));
EditText c = new EditText(this);
row.addView(t);
row.addView(c);
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
});
}
}
guys,i have a EditText and a Button,i want my app do this,user will enter a value and when button is clicked,new rows contain the EditText and TextView will be created. the number of new rows based on the input value. but i got a few errors at TableRow row = new TableRow(this);,TextView t = new TextView(this); and EditText c = new EditText(this);..why i cannot use "this"??
i modified this code from this
public class tablelayout extends Activity implements onclickListener {
/** Called when the activity is first created. */
//initialize a button and a counter
Button btn;
int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setup the layout
setContentView(R.layout.main);
// add a click-listener on the button
btn = (Button) findViewById(R.id.Button01);
btn.setonclickListener(this);
}
// run when the button is clicked
public void onclick(View view) {
// get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
// create a new TableRow
TableRow row = new TableRow(this);
// count the counter up by one
counter++;
// create a new TextView
TextView t = new TextView(this);
// set the text to "text xx"
t.setText("text " + counter);
// create a CheckBox
CheckBox c = new CheckBox(this);
// add the TextView and the CheckBox to the new TableRow
row.addView(t);
row.addView(c);
// add the TableRow to the TableLayout
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
please help me.... and if this success,how can i know the id of the new EditTexts if i want to manipulate them?

New Topic/Question
Reply



MultiQuote




|