How to setOnValueChangedListener from NumberPicker inside a BaseAdapter

后端 未结 1 1490
时光取名叫无心
时光取名叫无心 2021-01-17 05:49

I am trying to create a BaseAdapter which each element has a NumberPicker and a Button. The button\'s action parameters depends on the value picked in NumberPicker. One solu

1条回答
  •  心在旅途
    2021-01-17 06:22

    Since the adapter will reuse the layout, you will need to set your listener when you create the layout. Move the button listener out. Otherwise, it will get set on every value change. Here is the revised code:

    // Put the array as a member of your activity or fragment's class
    // Remember to save it between life cycle event
    // Initialize it to same size as your adapter data
    int[] numValues;
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null)
        {
            LayoutInflater vi = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
    
            final Button button = (Button) v.findViewById(R.id.button);
            final NumberPicker numPick = (NumberPicker) v.findViewById(R.id.numPick);
    
            button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View arg0) {
                        // Get the position value from the tag field
                        int pos = (int)button.tag;
                        actionToPerform(numValues[pos]);
                    }
                });
    
            numPick.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                  // Save the value in the number picker
                  numPick.tag = newVal;
              }
            });
        }
    
        // Save the position in the button's tag field
        button.tag = position;
        // Restore picker value
        final NumberPicker numPick1 = (NumberPicker) v.findViewById(R.id.numPick);
        numPicker1.setValue(numValues[position]);
    
    }
    

    Saving the data in the tag field will ensure you get the right row's data when the button is clicked. I don't know how your data is represented so I introduce a int array to store the values. Otherwise, the picker will lose the value when it gets reused.

    0 讨论(0)
提交回复
热议问题