Search code examples
androidtextviewiterationnumberpicker

Using an array int value outside For loop in Android


I'm trying to use the tvs[i] variable outside of the For loop in which it is generated, in another method, OnValueChange, to set a TextView value for NumberPickers. However, I'm only generating a TextView value for the FIRST tvs[i] NumberPicker, whichever NumberPicker I spin.

Can anyone explain where I've gone wrong in the below code? I've tried expanding For loop to below the OnChangeListener method, but I then have problems with tvs[i] not having a final value etc. What will link the initial tvs[i] values with the tvs[i].setText ??

public void setupUI()
{

    int[] textViewIDs = new int[] {R.id.tvId1, R.id.tvId2, R.id.tvId3, R.id.tvId7,
            R.id.tvId8, R.id.tvId9, R.id.tvId10, R.id.tvId11, R.id.tvId12, R.id.tvId13, R.id.tvId14, R.id.tvId15, R.id.tvId16, R.id.tvId17, 
            R.id.tvId18, R.id.tvId19, R.id.tvId20, R.id.tvId21, R.id.tvId22, R.id.tvId23, R.id.tvId24, R.id.tvId25, R.id.tvId26, R.id.tvId27, R.id.tvId28, R.id.tvId29, R.id.tvId30, R.id.tvId31, 
            R.id.tvId32, R.id.tvId33, R.id.tvId34, R.id.tvId35, R.id.tvId36, R.id.tvId37, R.id.tvId38, R.id.tvId39, R.id.tvId40, R.id.tvId41, R.id.tvId42, R.id.tvId43, R.id.tvId44, R.id.tvId45, R.id.tvId46, R.id.tvId47, R.id.tvId48, R.id.tvId49, R.id.tvId50, R.id.tvId51, R.id.tvId52, R.id.tvId53};
    tvs = new TextView[53];
    for(int i=0; i < textViewIDs.length; i
            ++) {
         tv = (TextView ) findViewById(textViewIDs[i]);
         tvs[i] = tv;
    }

    //tv = (TextView) findViewById(R.id.tvId1);

    int[] numpickIDs = new int[] {R.id.npId1, R.id.npId2, R.id.npId3, R.id.npId7,
            R.id.npId8, R.id.npId9, R.id.npId10, R.id.npId11, R.id.npId12, R.id.npId13, R.id.npId14, R.id.npId15, R.id.npId16, R.id.npId17, 
            R.id.npId18, R.id.npId19, R.id.npId20, R.id.npId21, R.id.npId22, R.id.npId23, R.id.npId24, R.id.npId25, R.id.npId26, R.id.npId27, R.id.npId28, R.id.npId29, R.id.npId30, R.id.npId31, 
            R.id.npId32, R.id.npId33, R.id.npId34, R.id.npId35, R.id.npId36, R.id.npId37, R.id.npId38, R.id.npId39, R.id.npId40, R.id.npId41, R.id.npId42, R.id.npId43, R.id.npId44, R.id.npId45, R.id.npId46, R.id.npId47, R.id.npId48, R.id.npId49, R.id.npId50, R.id.npId51, R.id.npId52, R.id.npId53};

    for(int j=0; j < numpickIDs.length; j++) {
        NumberPicker np = (NumberPicker ) findViewById(numpickIDs[j]);


    //NumberPicker np = (NumberPicker) findViewById(R.id.npId1);

    np.setOnValueChangedListener(new OnValueChangeListener()
    {
        public void onValueChange(NumberPicker picker, int oldVal, 
            int newVal)
        {


            tvs[i].setText(String.valueOf(newVal)); 

        }        
    });

    np.setMaxValue(12);
    np.setMinValue(0);
    }

}

Solution

  • Before the setOnValueChangedListener call, add this line

    final TextView curTextView = tvs[j];
    

    Then change the onValueChanged to

    curTextView.setText(String.valueOf(newVal));