将onclicklistener设置为numberpicker dynammicaly添加了android

时间:2013-06-01 14:30:01

标签: android dynamic event-listener

我有以下情况:

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

那部分工作,但是当我想将eventlistener添加到numberpicker时,所以当他们点击+或 - 时,它会为te票价加算合适的价格。

enter image description here

就像你在图片上看到的那样,动态添加了5个textviews和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);

                            }

                        }
                    });

1 个答案:

答案 0 :(得分:0)

你可以做的是getParent NumberPicker ...然后在那个parentView中获取带有childPosition的textView,根据你的要求更新......

喜欢这个..

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");

            }
        });
    }