2013-08-16 100 views
0

我想爲Spinner做其他語句。 我宣佈「六一」,「七一」,「八一」,「九月」,「月」,「月」,「月」Spinner if else語句

當我點擊「八一」它會給我MainActivity.java 其他它會給我August.java。 現在, 我的微調是自動選擇, 這意味着,當我加載頁面時,它會自動爲我選擇「6月」。 有沒有辦法讓我禁用自動選擇?

這是我的代碼我date.java

//SpinnerView 
      s1 = (Spinner) findViewById(R.id.spinner1); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, months); 
     s1.setAdapter(adapter); 
     s1.setOnItemSelectedListener(new OnItemSelectedListener() 
     { 
      public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) { 
       int index = s1.getSelectedItemPosition(); 
       //Toast.makeText(getBaseContext(), "You have seleted item :" + months[index] , Toast.LENGTH_SHORT).show(); 
      } 
      public void onNothingSelected(AdapterView<?>arg0) {} 
      }); 


    if (s1.equals("August")) { 

     startActivity(new Intent(date.this,MainActivity.class)); 
    } 
    else{ 
     startActivity(new Intent(date.this,august 
       .class)); 

    } 
+0

你爲什麼不改變的String []月= { 「六一」, 「七月」, 「八一」, 「Septemeber」, 「十一月」 「十二月」}作爲的String []月= { 「選擇」 「六一」, 「七月」, 「八一」, 「Septemeber」, 「十一月」, 「十二月」}; ??? – LMK

+0

謝謝!(:我會更改它 – randomize

回答

1

試試這個:

添加陣列,如 「選擇月」, 「月」, 「七五」, 「八五」, 「九月」, 「月」, 「月」, 「月」

並在登記

s1.setOnItemSelectedListener(new OnItemSelectedListener() 
     { 
      public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) { 
       int index = s1.getSelectedItemPosition(); 
       if(index>0) 
       { 
          String Month = MonthArray[index]; 
        if (Month.equalIgnoreCase("August")) { 

         startActivity(new Intent(date.this,MainActivity.class)); 
        } 
        else{ 
         startActivity(new Intent(date.this,august 
           .class)); 

        } 
       } 
      } 
      public void onNothingSelected(AdapterView<?>arg0) {} 
     }); 

希望它有助於!

+0

它仍然不能正常工作 – randomize

+0

嘗試編輯的代碼。從幾個月的數組獲取字符串,然後檢查它是否等於「八月」或不? –

+0

可以看看這個問題? http://stackoverflow.com/questions/18266824/spinner-with-list-view – randomize

0

String [] months = { 
       "June", 
       "July", 
       "August", 
       "Septemeber", 
       "November", 
       "December", 

部分需要將Spinner的最後選擇的項目位置無論是存儲在您的數據庫或SharedPreference。加載應用程序時,請檢查您的數據庫中是否有商品的位置或SharedPreference,然後根據此位置選擇Spinner中的商品。

1

可能,這將有助於

import java.util.ArrayList; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 

public class date extends Activity implements AdapterView.OnItemSelectedListener{ 



    ArrayList<String> months; 

    Spinner spinner; 

    ArrayAdapter<String> month_adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     months = new ArrayList<String>(); 

     months.add("Select"); 
     months.add("June"); 
     months.add("July"); 
     months.add("August"); 
     months.add("Septemeber"); 
     months.add("November"); 
     months.add("December"); 

     spinner = (Spinner) findViewById(R.id.spinner); 
     spinner.setOnItemSelectedListener(this); 


     month_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, months); 
     month_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(month_adapter); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> arg0, View v, int position, 
      long arg3) { 
     // TODO Auto-generated method stub 


     if(!(spinner.getSelectedItem().toString().trim().equals("Select"))) 
     { 

      if (spinner.getSelectedItem().toString().trim().equals("August")) { 

       startActivity(new Intent(date.this,MainActivity.class)); 
      } 
      else{ 
       startActivity(new Intent(date.this,august 
          .class)); 

      } 

     } 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 

} 
+0

可以看看這個問題嗎? http://stackoverflow.com/questions/18266824/spinner-with-list-view – randomize