2016-02-18 51 views
0

我試圖在Spinner中選擇一個項目,當點擊發送按鈕時該項目將打開一個活動。例如,活動1和活動2.在我的微調中,我有項目1和項目2.當我選擇項目1時,我希望活動1打開。 我試過一些代碼,但它不工作。 這是我的代碼。如何在Spinner中選擇項目以打開新的活動

微調在activity_main.xml中

<Spinner 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/spinner" 
     android:layout_alignBottom="@+id/button" 
     android:layout_toRightOf="@+id/button" 
     android:layout_toEndOf="@+id/button" 
     android:entries="@array/punpColleges" 
     android:spinnerMode="dropdown" /> 

在我MainActivity.java

public class MainActivity extends ActionBarActivity { 
    Spinner spin = (Spinner) findViewById(R.id.spinner); 
    private static Button button_send; 

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

     button_send = (Button) findViewById(R.id.button); 
     button_send.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         Intent intent = new Intent(); 
         if (spin.getSelectedItem().toString().equals("CCS")) { 
          startActivity(new Intent(MainActivity.this, ListActivity.class)); 
         } else { 
          startActivity(new Intent(MainActivity.this, SecondTesting.class)); 
         } 
        } 
       } 
     ); 

} 

字符串位於strings.xml中

<string-array name="punpColleges"> 
     <item>CCS</item> 
     <item>CBE</item> 
    </string-array> 

我也是在AndroidManifest.xml中添加了此

<intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".ListActivity" 
      android:label="@string/title_activity_list" > 
      <intent-filter> 
       <action android:name="android.intent.action.ListActivity" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 

在此先感謝您的幫助!

+0

安置自己的代碼微調 – Jas

+0

你是什麼意思它不工作?並在調用'setContentView'方法後移動'spin =(Spinner)findViewById(R.id.spinner);'方法 –

+0

當我在智能手機上運行我的應用程序時,它不起作用。 –

回答

0

你不能在類級別初始化微調。必須在致電setContentView後完成。除非您已將佈局設置爲活動,否則不能使用findViewById

這樣定義

Spinner spin; 

微調添加

spin = (Spinner) findViewById(R.id.spinner); 
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_spinner_dropdown_item, 
      getResources().getStringArray(R.array.punpColleges)); 
spin.setAdapter(spinnerAdapter); 

onCreate調用setContentView

+0

在我的if語句中如何?有沒有錯誤? –

+0

如果塊對我來說很好看。您需要按照我的建議更新您的代碼。 – Rohit5k2

+0

它的工作原理!非常感謝!^_^ –

0

在點擊按鈕試試這個打電話給你的活動

try { 
     myClass = Class.forName("com.example.yourPackageName." + classComplete); //classcomplete as your file name, take it from spinner selected object 
     Intent myIntent = new Intent(MainActivity.this, myClass); 
     startActivity(myIntent); 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
0

定義微調和變量來存儲微調的位置後

int x=0; 
Spinner spin=(Spinner) findViewById(R.id.spinner); 
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, 
     android.R.layout.simple_spinner_dropdown_item, 
     list); 
spin.setAdapter(spinnerAdapter); 
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, 
            View view, int position, long id) { 
               x=position; 
}); 
} 

現在比較上按鈕的點擊現在的位置

if(x==0) 
Intent myIntent = new Intent(MainActivity.this, myClass1); 
    startActivity(myIntent); 
else 
Intent myIntent = new Intent(MainActivity.this, myClass2); 
    startActivity(myIntent); 
相關問題