Android - 如何在更改微调器项目时更改视图?

时间:2017-01-21 23:49:38

标签: android spinner android-view

这有点难以解释,但我会尽我所能!

我有一个数字从1到50的微调器。当我点击一个数字时,我写的脚本创建了许多TableView,就像我点击的数字(例如:点击3 - > 3 TableViews)。

当我点击另一个号码时会出现问题:我希望将我之前的视图替换为新号码,而不是在其末尾添加! 只是为了更好地解释:我点击3并创建3个视图;然后我点击4:我想现在有4个视图,但它给了我7个,因为它确实有3 + 4个。如果我现在点击50,我将有57个视图而不是50个等等...真的不知道如何使这个工作。

感谢您的帮助!

这是完整的代码,但我确定你对我在OnItemSelectedListener中所做的事情不感兴趣...无论如何我会在这里发布它,以防你需要它!

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){}
    });

1 个答案:

答案 0 :(得分:1)

在开始添加新表之前,您应该删除表中的所有子项:

//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++) {
    ...
}