如何为循环

时间:2016-10-03 11:20:43

标签: android

我通过for循环动态创建了视图。它还包含多个微调器。我使用onItemSelectedListener为微调器选择值。每当用户单击微调器时,我都使用了一些逻辑来保存数据库中的微调器值,但是甚至在单击for循环内的微调器之前就会调用onItemSelectedListener。

如何在单击时调用微调器方法,而不是为每个for循环迭代调用?

//set the questions for the user dynamically with the for loop
    for (int ss = 0; ss < totalNoOfQuestions.size(); ss++) {
        pos = ss;
        final List<String> list = new ArrayList();
        list.add("SELECT");
        TextView textView = new TextView(activity);
        textView.setTextSize(15);
        textView.setTextColor(view.getResources().getColor(R.color.black));
        textView.setTypeface(typeface1);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 20, 0, 0);
        textView.setLayoutParams(params);

        String questText = totalNoOfQuestions.get(ss).getParagraphQuestion();
        final String questionNum = totalNoOfQuestions.get(ss).getQuestionNo();
        String question = totalNoOfQuestions.get(ss).getQuestions();
        textView.setText(questText + " " + questionNum + "   " + question);

        spinner[ss] = new Spinner(activity);
        for (int i = 0; i < totalNoOfQuestions.get(0).getOptions().size(); i++) {
            String option = totalNoOfQuestions.get(0).getOptions().get(i).getQuestionOptionNo();
            list.add(option);
        }
        spinner[ss].setId(ss);
        spinner[ss].setMinimumWidth(100);

        //spinner.setBackground(activity.getResources().getDrawable(R.drawable.background_gradient));
        arrayAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_dropdown_item, list);
        spinner[ss].getBackground().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_ATOP);
        //spinner.setBackground(activity.getResources().getDrawable(R.drawable.background_gradient));
        spinner[ss].setAdapter(arrayAdapter);

        //retrieve value from the database if user already selected
        List<QuestionsNAnswers> inventories = getAll();
        for(int k = 0 ; k<inventories.size() ;k++){
            QuestionsNAnswers questionsNAnswers = inventories.get(k);
            String quesNo = questionsNAnswers.questionNo;
            spinner[ss].setSelection(Integer.parseInt(quesNo));
        }

        final int finalSs = ss;
        spinner[ss].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                // Toast.makeText(activity, "You Selected " + selection, Toast.LENGTH_SHORT).show();
                int spinnerId = spinner[finalSs].getId();
                questionNo = totalNoOfQuestions.get(spinnerId).getQuestionNo();
                optionNo = spinner[finalSs].getItemAtPosition(position).toString();

                //save the options in the database
                QuestionsNAnswers questionsNAnswers = new QuestionsNAnswers();
                questionsNAnswers.questionNo = questionNo;
                questionsNAnswers.optionSelected = optionNo;
                //save the records in the database
                questionsNAnswers.save();


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        linearLayoutForQuestions.addView(textView);
        linearLayoutForQuestions.addView(spinner[ss]);
    }

1 个答案:

答案 0 :(得分:0)

这不是最佳解决方案,但应该有效。 做另一个监听器ontouchlistener,它改变了一些值,并在微调器上执行。 然后在微调器内,仅当值为true并重置值时才向db添加值。