Android - 如何在微调项更改时更改视图?
Android - How to change the view when a spinner item is changed?
这会有点难以解释,但我会尽力而为!
我有一个数字从 1 到 50 的微调器。当我点击一个数字时,我编写的脚本会创建一些 TableView,就像我点击的数字一样(例如:点击 3 --> 3 TableView) .
当我点击另一个号码时出现问题:我想用新号码替换我以前的视图,而不是在它的末尾添加!
只是为了更好地解释:我点击 3,它创建了 3 个视图;然后我点击 4:我现在想有 4 个视图,但它给了我 7,因为它有 3+4。如果我现在点击 50,我将有 57 个视图,而不是 50 个等等...真的不知道如何进行这项工作。
感谢您的帮助!
这是完整的代码,但我确定您对我在 OnItemSelectedListener 中所做的事情不感兴趣...无论如何,我会 post 在这里,以防万一您需要它!
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){}
});
在开始添加新的之前,您应该删除 table 的所有子项:
//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++) {
...
}
这会有点难以解释,但我会尽力而为!
我有一个数字从 1 到 50 的微调器。当我点击一个数字时,我编写的脚本会创建一些 TableView,就像我点击的数字一样(例如:点击 3 --> 3 TableView) .
当我点击另一个号码时出现问题:我想用新号码替换我以前的视图,而不是在它的末尾添加! 只是为了更好地解释:我点击 3,它创建了 3 个视图;然后我点击 4:我现在想有 4 个视图,但它给了我 7,因为它有 3+4。如果我现在点击 50,我将有 57 个视图,而不是 50 个等等...真的不知道如何进行这项工作。
感谢您的帮助!
这是完整的代码,但我确定您对我在 OnItemSelectedListener 中所做的事情不感兴趣...无论如何,我会 post 在这里,以防万一您需要它!
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){}
});
在开始添加新的之前,您应该删除 table 的所有子项:
//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++) {
...
}