2017-09-22 8 views
0

私はクライアントに登録するために私のアプリにフォームを持っています。そのフォームで私はYesまたはNoを持っているConsentドロップダウンを持っています。私がしたかったのは、人がNoを選択して登録を完了できない場合、Yesを選択して次のアクティビティに進む必要があるということです。私のコードでは、ユーザーが何を選択したかにかかわらず、スティルはエラーメッセージを表示し、ユーザーは移動を許可しません。Android - Spinnerエラーメッセージを作成する

私はこのために使用されるコードは次のとおりです。セットアップ

Arrayアダプタ:

//set up for consent spinner 
    adapter = ArrayAdapter.createFromResource(this, 
      R.array.Register_array_consent, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    mspinnerPatientConsent.setAdapter(adapter); 

スピナーエラーが設定:

// Consent Spinner Error set up 
    Boolean no_error; 
    TextView errorText = (TextView)mspinnerPatientConsent.getSelectedView(); 
    if (mspinnerPatientConsent.getSelectedItem().toString().contains("No"));{ 
     errorText.setText("Patient Must Consent to Be Registered"); 
     errorText.setError(""); 
    } 
    if (mspinnerPatientConsent.getSelectedItem().toString().contains(" "));{ 
     errorText.setText("Patient Must Consent to Be Registered"); 
     errorText.setError(""); 
     no_error = false; 
    } 

を私は本当に新しいですそれはおそらく私が問題を見ることができない理由です。私は有用な入力を感謝します。ありがとうございました!

答えて

0

の代わりに、選択した項目の文字列を使用して、この選択した項目のインデックスを返します

mspinnerPatientConsent.getSelectedItemPosition() 

を使用してください。

if (mspinnerPatientConsent.getSelectedItemPosition()==0);{ 
    errorText.setText("Patient Must Consent to Be Registered"); 
    errorText.setError(""); 
} 
if (mspinnerPatientConsent.getSelectedItemPosition()==1);{ 
    errorText.setText("Patient Must Consent to Be Registered"); 
    errorText.setError(""); 
    no_error = false; 
} 
+0

「if」ステートメントでこれを変更する必要がありますか? –

+0

このコードはif文に入れる必要があります。私の編集した答えを見てください。 – rmdan

+0

これは動作しませんでしたが、選択に関係なくエラーが表示されました –