2012-01-30 7 views
10

は、私は現在、私のAndroidアプリケーションのためのアカウント管理活動に取り組んでいると私はスピナーからsetSelection()方法が接続OnItemSelectedListenerをトリガしない理由を考え出すのトラブルを抱えているスピナーは言い。Spinner.setSelectionがOnItemSelectedListenerトリガされません正しく

これは私が現在持っているものです。

のonCreate()メソッド:

/** OnItemSelectedHandler for the Country Spinner */ 
    mCountrySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

     public void onItemSelected(AdapterView<?> parent, View view, 
       int pos, long id) { 
      Log.i(TAG, "onCountrySelected() was called, position : " + pos); 

      mProvinces = new ArrayList<String>(); 
      mProvincesCode = new ArrayList<String>(); 

      mXML.parseResponse(FileManager.getInstance().getPortalOptions()); 

      for (int i = 0; i < mXML.getCountry(pos).sizeProvinces(); i++){ 
       mProvinces.add(mXML.getCountry(pos).getProvince(i).getLabel(mLanguage)); 
       mProvincesCode.add(mXML.getCountry(pos).getProvince(i).getCode()); 
      } 

      mProvinceArrayAdapter = new ArrayAdapter<String>(ManageAccountActivity.this, 
        android.R.layout.simple_spinner_item, mProvinces); 
      mProvinceArrayAdapter.setDropDownViewResource(
        android.R.layout.simple_spinner_dropdown_item); 
      mProvinceSpinner.setAdapter(mProvinceArrayAdapter); 
     } 

     public void onNothingSelected(AdapterView<?> arg0) { 
      // Do Nothing ...    
     } 
    }); 

そして再び:リスナースピナーの結合を示しているアクティビティの作成時に呼び出されたinitializeUI()メソッドから

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.account_management); 

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

    retreiveLanguage(); 
    initializeUI(); 

    // Vérification si l'usager est déjà connecté 
    Globals appState = ((Globals) this.getApplication()); 
    boolean userLoggedIn = appState.isUserLoggedIn(); 
    boolean userInfoAvailable = appState.isUserInfoAvailable(); 

    if (userLoggedIn && userInfoAvailable) { 
     fillUI(); 
    } 
} 

関連のある行fillUIメソッド()からの別のカップルライン:

Log.i(TAG, "Setting country based on user information."); 
((Spinner) findViewById(R.id.spin_country)) 
    .setSelection(mCountriesCode.indexOf(mUser.getCountry())); 
// TODO : Fix Provinces and States not being changed accordingly 
Log.i(TAG, "Setting province based on user information."); 
((Spinner) findViewById(R.id.spin_province)) 
    .setSelection(mProvincesCode.indexOf(mUser.getProvince())); 

私はfillUI()メソッドで選択をセットした直後にOnItemSelectedListenerが呼び出されることを期待していますが、それは実行時に起こっていることではありません:S

私のLogCatの抽出は、選択は国スピナーに適用されます。

I/ManageAccountActivity(28108):ユーザー情報に基づいて国を設定します。

I/ManageAccountActivity(28108):ユーザー情報に基づいて地域を設定します。

I/ManageAccountActivity(28108):onCountrySelected()、位置と呼ばれていました:実験として1

、私も私の活動のONSTART方法が、そののdidnにfillUI()の呼び出しを入れてみました」アプリケーションがどのように反応したかを変更します。

ご指摘いただきありがとうございました。

+0

、OnneremSelectedListenerは、スピナーで何らかのアクションを実行したときにのみ起動されます。 –

+1

私はそれを変更します...選択を0から1に変更すると変更されないと考えられますか? –

答えて

19

あなたはブール値を使用して、二つの引数を使って、スピナーを設定するために、第2を試してみました:

.setSelection(mProvincesCode.indexOf(mUser.getProvince()), true); 

developers pageから、それは示しています

setSelection(int position, boolean animate) 
//Jump directly to a specific item in the adapter data. 
+0

私はanimate引数をfalseにして試しました(別のスレッドから取得しました)引数をtrueに設定して試してみます。 –

+3

本当にありがとう、私は最初にそれを真に設定しようとしていたはずです。両方のスピナーをsetSelection(pos、true)に設定すると、そのトリックが実行されました。 –

+0

間違っていると、 'animate'パラメータを設定すると、OnItemSelectedListenerが起動されません。 –

3

ただ、次のコードを使用します。

ownerSpinnerVw.post(new Runnable() { 
     @Override 
     public void run() { 
      ownerSpinnerVw.setSelection(position); 
     } 
    }); 
0

この問題を解決する方法は、これをonCreateメソッドに追加しています。プログラムは動作しますが、最初の選択のためだけです。 2回目にプログラムを選択すると、エミュレータがクラッシュします。

spinner.setOnItemSelectedListener(this); 

enter image description here

+0

あなたの投稿に記載されています。この回答は機能していません。コメントとして追加してください。 – Arashsoft

0

私はあなたが前に

yourSpinner.setOnItemSelectedListener(null); 

を宣言した場合setSelection(POS)が動作することがわかりました。

関連する問題