2014-03-19 21 views
0

我在代碼中創建了一個微調,如下所示。我如何將它連接到另一個活動(比如Bangalore.java)。我嘗試了一些可用的stackoverflow,但它不工作。如何將活動鏈接到微調元素?

package com.example.searchbox; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import com.example.searchbox.run; 
import com.example.searchbox.ArrayList; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     String[] cityNames = {"Jaipur","Bangalore","Agra"}; 
     setContentView(R.layout.activity_main); 
     Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
     spinner.setAdapter(new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, 
        cityNames)); 
     /*how can i connect activity of the spinner to another activity*/ 

    } 


    @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; 
    } 

} 
+0

從微調獲得所選擇的項目和使用意圖值傳遞給下一個活動,如果我正確地理解你的問題 – Raghunandan

回答

0

你可以做這樣的事情:

MyOnItemSelectedListener myonitemselectedlistener =new MyOnItemSelectedListener (savedRoomNames); 
    YOURSPINNER.setOnItemSelectedListener(myonitemselectedlistener); 

然後:

private class MyOnItemSelectedListener implements OnItemSelectedListener{ 
    AdapterView<?> arg0; 
    View arg1; 
    int arg2; 
    long arg3; 

    public MyOnItemSelectedListener(String[] gespeicherteRaeume) { 
     this.savedRooms=gespeicherteRaeume; 
    } 
    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 
     this.arg0=arg0; 
     this.arg1=arg1; 
     this.arg2=arg2; 
     this.arg3=arg3; 

     //here you have to look which item is arg2 and then if == yyour item start your new activity via intent 

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

我會使用OnItemSelectedListener,然後在正確的項目被捕獲後啓動活動。