【问题标题】:How to set onitemSelectedListener for the spinner inside for loop如何为 for 循环内的微调器设置 onitemSelectedListener
【发布时间】:2016-10-03 21:05:36
【问题描述】:

我已经通过 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]);
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    这不是最好的解决方案,但应该可以。 做另一个监听器 ontouchlistener 改变一些值并在微调器上执行单击。 然后在 spinner 中,仅当 value 为 true 时才向 db 添加 value 并重置 value。

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多