2011-12-19 18 views
3

アンドロイド関連スピナーの実行方法は?アンドロイド関連スピナー

私はこのようにそれを行っている:

プライベートスピナーddlCountry、ddlCategory。 onItemSelectedを繰り返しますすなわち

@Override 
public void onCreate(Bundle savedInstanceState) { 
    try { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.pnplandingpage); 

     ddlCountry = (Spinner) findViewById(R.id.ddlCountry); 
     ddlCategory = (Spinner) findViewById(R.id.ddlCategory); 

     // BindCountry(ddlCountry); 

     // ddlCountry 
     // .setOnItemSelectedListener(new OnCountryItemSelectedListener()); 

     // ddlCategory 
     // .setOnItemSelectedListener(new 
     // OnCategoryItemSelectedListener()); 

     // ArrayAdapter<CharSequence> adapter = ArrayAdapter 
     // .createFromResource(this, R.array.Country_array, 
     // android.R.layout.simple_spinner_item); 
     // adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     // ddlCountry.setAdapter(adapter); 

     ddlCountry.setOnItemSelectedListener(new OnItemSelectedListener() { 
      public void onItemSelected(AdapterView<?> parent, View pview, 
        int pos, long id) { 

       final String[] array = new String[] { "Category", 
         "5 Miles", "10 Miles", "15 Miles", "20 Miles", 
         "25 Miles", }; 

       List<String> list = new ArrayList<String>(); 
       for (int i = 0; i < array.length; i++) { 
        list.add(array[i]); 
       } 

       ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
         PNPLandingPage.this, 
         android.R.layout.simple_spinner_item, list); 
       dataAdapter 
         .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       ddlCategory.setAdapter(dataAdapter); 

       Toast.makeText(
         parent.getContext(), 
         "The Country is " 
           + parent.getItemAtPosition(pos).toString(), 
         Toast.LENGTH_LONG).show(); 
      } 

      public void onNothingSelected(AdapterView parent) { 
       // Do nothing. 
       ddlCategory.setAdapter(null); 

      } 
     }); 

     // BindCategory(ddlCategory); 

     // ddlCategory 
     // .setOnItemSelectedListener(new OnCategoryItemSelectedListener()); 

     ddlCategory.setOnItemSelectedListener(new OnItemSelectedListener() { 
      public void onItemSelected(AdapterView<?> parent, View pview, 
        int pos, long id) { 

       final String[] array = new String[] { "Country", 
         "Country1", "Country2", "Country3", "Country4", 
         "Country5", }; 


       List<String> list = new ArrayList<String>(); 
       for (int i = 0; i < array.length; i++) { 
        list.add(array[i]); 
       } 

       ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
         PNPLandingPage.this, 
         android.R.layout.simple_spinner_item, list); 
       dataAdapter 
         .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       ddlCountry.setAdapter(dataAdapter); 

       // ddlCategory 
       // .setOnItemSelectedListener(new 
       // OnCategoryItemSelectedListener()); 

       Toast.makeText(
         parent.getContext(), 
         "The Category is " 
           + parent.getItemAtPosition(pos).toString(), 
         Toast.LENGTH_LONG).show(); 
      } 

      public void onNothingSelected(AdapterView parent) { 
       // Do nothing. 
       ddlCountry.setAdapter(null); 

      } 
     }); 

    } catch (Exception ex) { 
     Toast.makeText(getApplication(), "EXCEPTION :" + ex, 1000); 
    } 
} 

しかし、トーストは、毎回トリガーされます。何が私はあなたが任意のスピナーにsetOnItemSelectedListener()を設定したときに、それが発火するよう

+0

トーストが表示されますか? – Richa

+0

最初に親トーストが表示されてからカテゴリが表示されます。これはループとして続きます。 –

答えて

0

私は問題があると思い、

デフォルトでは(あなたがそれに割り当てられているか、または配列)、リストの最初の項目を選択します..だから間違いをしていますonItemSelected()が選択されています。

あなたのケースでは、両方のスピナーがItemItem()メソッドを実行しています。したがって、それらは無限ループの一種です。

編集回答

final String[] array = new String[] 
     { "Country", "Country1", "Country2", "Country3", "Country4", "Country5", }; 

     List<String> list = new ArrayList<String>(); 
     for (int i = 0; i < array.length; i++) 
     { 
      list.add(array[i]); 
     } 

     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(TwoSpinners.this, android.R.layout.simple_spinner_item, list); 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ddlCountry.setAdapter(dataAdapter); 

     ddlCountry.setOnItemSelectedListener(new OnItemSelectedListener() 
     { 
      public void onItemSelected(AdapterView<?> parent, View pview, int pos, long id) 
      { 
       if (!parent.getItemAtPosition(pos).toString().equals("Country")) 
       { 
        final String[] array = new String[] 
        { "Category", "5 Miles", "10 Miles", "15 Miles", "20 Miles", "25 Miles", }; 

        List<String> list = new ArrayList<String>(); 
        for (int i = 0; i < array.length; i++) 
        { 
         list.add(array[i]); 
        } 

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(TwoSpinners.this, android.R.layout.simple_spinner_item, list); 
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        ddlCategory.setAdapter(dataAdapter); 

        Toast.makeText(parent.getContext(), "The Country is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); 
       } 
      } 

      public void onNothingSelected(AdapterView parent) 
      { 
       // Do nothing. 
       ddlCategory.setAdapter(null); 

      } 
     }); 

     // BindCategory(ddlCategory); 

     // ddlCategory 
     // .setOnItemSelectedListener(new OnCategoryItemSelectedListener()); 

     ddlCategory.setOnItemSelectedListener(new OnItemSelectedListener() 
     { 
      public void onItemSelected(AdapterView<?> parent, View pview, int pos, long id) 
      { 

       if (!parent.getItemAtPosition(pos).toString().equals("Category")) 
       { 

        final String[] array = new String[] 
        { "Country", "Country1", "Country2", "Country3", "Country4", "Country5", }; 

        List<String> list = new ArrayList<String>(); 
        for (int i = 0; i < array.length; i++) 
        { 
         list.add(array[i]); 
        } 

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(TwoSpinners.this, android.R.layout.simple_spinner_item, list); 
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        ddlCountry.setAdapter(dataAdapter); 

        // ddlCategory 
        // .setOnItemSelectedListener(new 
        // OnCategoryItemSelectedListener()); 

        Toast.makeText(parent.getContext(), "The Category is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); 
       } 
      } 

      public void onNothingSelected(AdapterView parent) 
      { 
       // Do nothing. 
       ddlCountry.setAdapter(null); 

      } 
     }); 
+0

はい、あなたは正しいです、それは無限ループを作成します。選択した項目もこのループのために変更されます。私はカスケードドロップダウンのように私は子供がそれに応じて読み込まれる必要があります親を変更するとしようとしているときに私はそれを処理することができますどのように私はこれを避けることができます変更する必要があります子供の親を選択しますか? –

+0

スピナーの読み込み中にonItemSelected fuction呼び出しを回避する方法は? –

+0

同じデータをスピナーにロードするたびに、他のスピナーの選択に基づいてリストを作成する必要があります。 – AB1209

0

私はまったく同じinfiteループを持っていました。 onItemSelected()setContentView()と私のクラスメソッドsetupSpinner()で呼び出すと、この方法では、私は再びonItemSelected()を呼び出すmspinner.setOnItemSelectedListener(this)を行う...私のハンドラで固定

handler.postDelayed(myRunnable, 500) 500ミリ秒の遅延で。ランナブルには、mspinner.setOnItemSelectedListener(this)

関連する問題