Reputation: 1711
This will be a little hard to explain, but I'll do my best!
I've got a spinner with numbers from 1 to 50. When I tap on a number, the script I wrote creates a number of TableViews like the number I tapped (eg: tap 3 --> 3 TableViews).
The problem occurs when I tap to another number: I want to have my previous view replaced with the new number, instead of being added at the end of it! Just to explain better: I tap 3 and it creates 3 views; then I tap 4: I want to have 4 views now, but it gives me 7 because it does 3+4. If i tap 50 now, I'll have 57 views instead of 50 and so on...really dunno how to make this work.
Thanks for the help!
This is the full code but I'm sure you're not interested in what I do inside my OnItemSelectedListener...anyway I'll post it here, just in case you need it!
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
for (int j = 1+pokeIds.size(); j <= (int)spinner.getSelectedItem()+pokeIds.size(); j++) {
//get a reference for the TableLayout
TableLayout table = (TableLayout)findViewById(R.id.internalcopies);
//create a new TableLayout
TableLayout internaltable = new TableLayout(getApplicationContext());
// create a new TableRow
TableRow row = new TableRow(getApplicationContext());
TableRow attackRow = new TableRow(getApplicationContext());
TableRow ultiRow = new TableRow(getApplicationContext());
ImageView iv = new ImageView(getApplicationContext());
iv.setImageResource(imageAdapter.mThumbIds[pokeID-1]);
TableRow.LayoutParams params = new TableRow.LayoutParams(200,200);
iv.setLayoutParams(params);
attackSpinner = new Spinner(getApplicationContext());
ultiSpinner = new Spinner(getApplicationContext());
attackSpinner.setBackgroundColor(getResources().getColor(R.color.erba));
ultiSpinner.setBackgroundColor(getResources().getColor(R.color.erba));
params = new TableRow.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
attackSpinner.setLayoutParams(params);
ultiSpinner.setLayoutParams(params);
attackSpinner.setId(j);
ultiSpinner.setId(j*10);
editText = new EditText(getApplicationContext());
if (dpi == 480) {
editText.setWidth(250);
editText.setTextSize(13);
}
else if (dpi == 420)
editText.setWidth(300);
editText.setHint("(Nome)");
editText.setHintTextColor(getResources().getColor(R.color.acciaio));
editText.setTextColor(getResources().getColor(android.R.color.black));
editText.setId(j*11);
List<String> attacks = pokemonHelper.getMoves(pokeID, "HasAttack");
ArrayAdapter<String>attacksAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, attacks);
attacksAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
attackSpinner.setAdapter(attacksAdapter);
List<String> ultis = pokemonHelper.getMoves(pokeID, "HasUlti");
ArrayAdapter<String>ultiAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, ultis);
attacksAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ultiSpinner.setAdapter(ultiAdapter);
// add the TextView to the new TableRow
params.gravity = Gravity.CENTER_VERTICAL;
params.setMargins(0,10,0,0);
row.addView(iv);
editText.setLayoutParams(params);
row.addView(editText);
attackRow.addView(attackSpinner);
ultiRow.addView(ultiSpinner);
internaltable.addView(attackRow);
internaltable.addView(ultiRow);
internaltable.setLayoutParams(params);
row.addView(internaltable);
// add the TableRow to the TableLayout
table.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
}
@Override
public void onNothingSelected(AdapterView<?> parent){}
});
Upvotes: 0
Views: 693
Reputation: 714
You should remove all the children of the table before you start adding new ones:
//get a reference for the TableLayout
TableLayout table = (TableLayout)findViewById(R.id.internalcopies);
table.removeAllViews();
for (int j = 1+pokeIds.size(); j < (int)spinner.getSelectedItem()+pokeIds.size(); j++) {
...
}
Upvotes: 1