0

我有以下情况:

当应用程序启动时,我会显示一个提示对话框,用户可以在其中说出他们想要多少张不同的票。如果他们填写 3,则会出现另一个对话框,他们可以在其中填写门票的描述和价格。

那部分有效,但是当我想将事件监听器添加到数字选择器时,当他们单击 + 或 - 它会计算门票的正确价格。

在此处输入图像描述

就像您在图像上看到的那样,动态添加了 5 个文本视图和 1 个数字选择器。

我尝试了以下方法,但这不起作用:

for (int i = 0; i < differentTickets; i++){
        final int j = i;
        NumberPicker np = (NumberPicker) findViewById(j+10);
        np.setOnNumberPickerChangedListener(new NumberPicker.OnNumberPickerChangedListener() {
            @Override
            public void onNumberPickerChanged(NumberPicker mynumberpicker) {
                TextView tv = (TextView) findViewById(j+100);
                tv.setText("test");

            }
        });
    }

这是我如何动态添加视图的代码:

for (int i = 0; i < differentTickets; i++) {
                                ticket t = new ticket();
                                t.setOmschrijving(omschrijvingenLijst
                                        .get(i).getText().toString());
                                t.setPrijs(Double.parseDouble(prijzenLijst
                                        .get(i).getText().toString()));
                                ticketten.add(t);

                                LinearLayout llTicketsH = new LinearLayout(
                                        MainActivity.this);
                                llTicketsH
                                        .setOrientation(LinearLayout.HORIZONTAL);

                                TextView tvv = new TextView(
                                        MainActivity.this);
                                tvv.setText(omschrijvingenLijst.get(i)
                                        .getText().toString());
                                tvv.setTextColor(Color.BLACK);
                                tvv.setTextSize(
                                        TypedValue.COMPLEX_UNIT_DIP, 30);
                                tvv.setWidth((int) convertDpToPixel(400,
                                        MainActivity.this));

                                llTicketsH.addView(tvv);

                                TextView tvPrijs = new TextView(
                                        MainActivity.this);
                                tvPrijs.setText(prijzenLijst.get(i)
                                        .getText().toString());
                                tvPrijs.setTextColor(Color.BLACK);
                                tvPrijs.setTextSize(
                                        TypedValue.COMPLEX_UNIT_DIP, 30);
                                tvPrijs.setWidth((int) convertDpToPixel(
                                        100, MainActivity.this));
                                tvPrijs.setGravity(Gravity.RIGHT);
                                tvPrijs.setId(i);
                                llTicketsH.addView(tvPrijs);

                                TextView tvEuro2 = new TextView(
                                        MainActivity.this);
                                tvEuro2.setText(" €");
                                tvEuro2.setTextColor(Color.BLACK);
                                tvEuro2.setTextSize(
                                        TypedValue.COMPLEX_UNIT_DIP, 30);
                                tvEuro2.setWidth((int) convertDpToPixel(50,
                                        MainActivity.this));
                                llTicketsH.addView(tvEuro2);

                                NumberPicker np = new NumberPicker(
                                        MainActivity.this, null);
                                np.setId(i+10);
                                llTicketsH.addView(np);

                                TextView tvPrijsTot = new TextView(
                                        MainActivity.this);
                                tvPrijsTot.setText("xxx");
                                tvPrijsTot.setTextColor(Color.BLACK);
                                tvPrijsTot.setTextSize(
                                        TypedValue.COMPLEX_UNIT_DIP, 30);
                                tvPrijsTot.setWidth((int) convertDpToPixel(
                                        100, MainActivity.this));
                                tvPrijsTot.setGravity(Gravity.RIGHT);
                                tvPrijsTot.setId(i+100);
                                llTicketsH.addView(tvPrijsTot);

                                TextView tvEuro = new TextView(
                                        MainActivity.this);
                                tvEuro.setText(" €");
                                tvEuro.setTextColor(Color.BLACK);
                                tvEuro.setTextSize(
                                        TypedValue.COMPLEX_UNIT_DIP, 30);
                                LinearLayout.LayoutParams MOs = new LinearLayout.LayoutParams(
                                        LayoutParams.MATCH_PARENT,
                                        LayoutParams.WRAP_CONTENT);
                                tvEuro.setLayoutParams(MOs);
                                llTicketsH.addView(tvEuro);

                                llTickets.addView(llTicketsH);

                            }

                        }
                    });
4

1 回答 1

0

你可以做的是getParent......NumberPicker然后在那个父视图中获取该视图中的文本childPosition视图,以根据您的要求进行更新......

像这样..

for (int i = 0; i < differentTickets; i++){
        final int j = i;
        NumberPicker np = (NumberPicker) findViewById(j+10);
        np.setOnNumberPickerChangedListener(new NumberPicker.OnNumberPickerChangedListener() {
            @Override
            public void onNumberPickerChanged(NumberPicker mynumberpicker) {
                //TextView tv = (TextView) findViewById(j+100);
                LinearLayout parView = (LinearLayout) mynumberpicker.getParent();
                TextView tv = parView.getChildAt(5);//your child view at position
                tv.setText("test");

            }
        });
    }
于 2013-06-01T15:22:28.173 回答