为什么我的变量递减?

时间:2017-03-19 03:51:11

标签: java android

while(i < numCourses){
            final int index = i;

            //Create a Dialog
            final Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.input_dialog);
            TextView textView = (TextView) dialog.findViewById(R.id.textView4);
            textView.setText("" + i);
            dialog.show();

            //Button for the dialog.
            Button b = (Button) dialog.findViewById(R.id.button);
            b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Get input Course Numbers, numCourses times
                    EditText editText = (EditText) dialog.findViewById(R.id.editText2); //EditText
                    String getInput = editText.getText().toString();             //String input
                    try {
                        courseNumbers[index] = Integer.parseInt(getInput);
                        Toast.makeText(MainActivity.this, "" + courseNumbers[index], Toast.LENGTH_SHORT).show();
                    }catch (Exception ex){}

                    //Dismiss dialog
                    dialog.dismiss();
                }
            });
            i++;
        }

这是我的变量int = i出现在Dialog中,假设numCourses是4:

3
2
1
0

我的问题是为什么向后递减,而不是从0递增到3。为了从对话框接收输入数组,我怎样才能有效地请求输入,numcourses次?而不是我有一个while循环和增加我,我觉得效率不高。

1 个答案:

答案 0 :(得分:1)

单击每个输入后,不使用循环,然后显示下一个对话框。

public void showCourseDialog(final int i, final int numCourses){
final int index = i;

        //Create a Dialog
        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.input_dialog);
        TextView textView = (TextView) dialog.findViewById(R.id.textView4);
        textView.setText("" + i);
        dialog.show();

        //Button for the dialog.
        Button b = (Button) dialog.findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Get input Course Numbers, numCourses times
                EditText editText = (EditText) dialog.findViewById(R.id.editText2); //EditText
                String getInput = editText.getText().toString();             //String input
                try {
                    courseNumbers[index] = Integer.parseInt(getInput);
                    Toast.makeText(MainActivity.this, "" + courseNumbers[index], Toast.LENGTH_SHORT).show();
                }catch (Exception ex){}

                //Dismiss dialog
                dialog.dismiss();

               if(i < numCourses)
                  showCourseDialog(++i, numCourses);
            }
        });

}