问题
protected void updateTable() {
// TODO Auto-generated method stub
final TableLayout tl = (TableLayout) findViewById(R.id.settingtable);
tl.removeAllViews();
for(int i=0; i<10; i++) {
final TableRow tablerow = new TableRow(this);
tablerow.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
final TextView deviceedit = new TextView(this);
deviceedit.setText(i+1);
deviceedit.setTextColor(Color.BLACK);
deviceedit.setGravity(Gravity.CENTER);
deviceedit.setTextSize(20);
deviceedit.setBackgroundResource(R.drawable.lighter_cell_shape);
tablerow.addView(deviceedit);
final EditText pondedit = new EditText(this);
pondedit.setText("");
pondedit.setTextColor(Color.BLACK);
pondedit.setGravity(Gravity.CENTER);
pondedit.setBackgroundResource(R.drawable.lighter_cell_shape);
tablerow.addView(pondedit);
pondedit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
tl.addView(tablerow);
}
}
When I entered text in generated edittexts, how can I get those texts from edittexts with references to those textViews i.e, 1-> hai , 2-> bye like this. Is TextWatcher useful for this context or not? I want to get data from edittexts and to save the data in database with reference to TextView.
回答1:
probably you will want to do this in separated for loops.
and to get the text entered in each EditText
you need something like this:
ArrayList<String> textFromEditText = new ArrayList<>();
textFromEditText.add(pondedit.getText().toString());
dont forget to call the .toString();
method since the from EditText
you get a Editable
type object.
once you have your data ready in the ArrayList just call this in another for loop:
for(i = 0;i < textFromEditText.size();i++){
final TextView deviceedit = new TextView(this);
//other code of your textView
deviceedit.setText(textFromEditText.get(i));
}
maybe somebody can complement my answer.
来源:https://stackoverflow.com/questions/14983122/i-want-to-implement-textwatcher-for-dynamically-generated-edittexts-and-want-to