2016-10-03 32 views
0

動的にforループを作成しました。複数のスピナーも含まれています。私は値を選択するために、スピナーにonItemSelectedListenerを使用しました。ユーザーがスピナーをクリックするたびに、データベースのスピナー値を保存するロジックを使用しましたが、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]); 
    } 

答えて

0

これは最適な解決策ではありませんが、うまくいくはずです。 値を変更し、スピンナーでperformClickを実行する別のリスナーontouchlistenerを実行します。 次に、スピナーの内側で、値がtrueでリセット値の場合にのみdbに値を追加します。

関連する問題