在Android中使用For循环外的数组int值

时间:2015-05-19 20:45:12

标签: android textview iteration numberpicker

我正在尝试在生成它的For循环之外使用OnValueChange变量,在另一种方法TextView中,为{{1}设置NumberPickers值}}。 但是,我只为FIRST TextView tvs[i]生成NumberPicker值,无论哪个NumberPicker旋转。

任何人都可以在下面的代码中解释我出错的地方吗?我已尝试将For循环扩展到OnChangeListener方法下方,但我遇到tvs[i]没有final值等问题。 什么会将初始tvs[i]值与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);
    }

}

2 个答案:

答案 0 :(得分:1)

在setOnValueChangedListener调用之前,添加此行

final TextView curTextView = tvs[j];

然后将onValueChanged更改为

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

答案 1 :(得分:0)

如果你想要更复杂的东西,你总是可以使用View类的“标签”功能。

基本上,您可以将每个TextView设置为关联NumberPicker的“标记”,以便onValueChange方法可以轻松访问并设置文本相关的TextView

在我的代码中,我也做了Activity实现NumberPicker.OnValueChangeListener,这使代码更清晰。

该示例在数组中显示了TextViewNumberPicker两个,但可以使用任何数字...

public class MainActivity extends Activity
    implements NumberPicker.OnValueChangeListener {

    private int[] textViewIDs = new int[] {R.id.tvId1, R.id.tvId2};
    private int[] numpickIDs = new int[] {R.id.npId1, R.id.npId2};
    private TextView[] tvs = new TextView[textViewIDs.length];
    private NumberPicker np;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupUI();
    }

    private void setupUI() {
        for (int i = 0; i < textViewIDs.length; i++) {
            tvs[i] = (TextView) findViewById(textViewIDs[i]);
            np = (NumberPicker) findViewById(numpickIDs[i]);

            // Set the TextView to be the "tag" of the NumberPicker
            np.setTag(tvs[i]);

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

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

        // Get the TextView of the NumberPicker and set
        // its text
        ((TextView)picker.getTag()).setText(String.valueOf(newVal));

    }
}