I encounter strange behavior, I suspect a mistake, but maybe I'm missing something. Code snippets at the end of the message. I can also publish .xml, but I don't think it is necessary to understand the problem.
I have a horizontal line layout that I add TableLayouts depending on the number of players.
Table layouts contain several fields: TextView, EditText, Buttons, etc.
When I initialize each TableLayout, I add it to LinearLayout (via xml inflation) and then initialize the fields.
The fact is that EditText fields are always updated with the information of the latest players. From what I can tell, my setText call is passed to each EditText.
I find EditText using findViewById in the parent TableLayout (in fact, grandpa and grandma, since there is a TableRow.
The EditText identifier is not unique throughout the tree, but unique in terms of table layout.
Possible reasons: 1) I get the wrong kind of EditText when I find ById: This may not be the case. I changed my code to publish the objectID for the EditTextView, I get IN A TEXTVIEW and get the unique identifiers of the objects. By sending the same myEditText.toString () to EditText, I get the duplicate value of the last identifier of the object. Repeat using the same findView logic when the field is a TextView and displays the "multiple" behavior of setText whenever it is EditText.
2) EditText TextView xml, EditText TextView, , .
3) , onClickListeners, , .
4) . , , , - .
:
1) - ?
2) ?
3) , , , , ( - TableLayout, .
CODE SNIPPETS:
onCreate Intialization Code:
TableLayout tl = (TableLayout)inflater.inflate(R.layout.playerpanel, llayout, false);
InitializePlayer(player1,tl);
llayout.addView(tl);
- - :
public void InitializePlayer(Player p, TableLayout tl)
{
players.add(p);
tl.setTag(p);
TextView scoreLabel = (TextView)tl.findViewById(R.id.currentScore);
EditText curPoints = (EditText)tl.findViewById(R.id.scoreEntry);
curPoints.setText(p.getScore());
EditText playerEntry = (EditText)tl.findViewById(R.id.playerName);
playerEntry.setText(tl.toString());
scoreLabel.setText(playerEntry.toString());
ListView lv = (ListView)tl.findViewById(R.id.listView);
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item, p.getTurns() ));
OnClickCode, :
private OnClickListener addButtonListener = new OnClickListener() {
public void onClick(View v) {
// get all the objects I care about
TableRow tr = (TableRow)v.getParent();
TableLayout tl = (TableLayout)tr.getParent();
EditText ev = (EditText)tl.findViewById(R.id.scoreEntry);
Player pl = (Player)tl.getTag();
TextView scoreLabel = (TextView)tl.findViewById(R.id.currentScore);
pl.addScore(ev.getText().toString());
scoreLabel.setText(pl.getScore());
ListView lv = (ListView)tl.findViewById(R.id.listView);
ArrayAdapter aa = (ArrayAdapter)lv.getAdapter();
aa.notifyDataSetChanged();
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
EditText playerEntry = (EditText)tl.findViewById(R.id.playerName);
playerEntry.setText(playerEntry.toString());
}
};