2016-11-08 235 views
-3

只要執行setOnClickListener我想開始另一個活動並將變量cn.getID()傳送給它。當在其他活動中我想immidietaly啓動方法findlocation並給它。如何啓動另一個活動並在其中啓動一個方法?

方法findLocation尚未完成。這個想法是,一旦獲得其他活動按鈕的ID,我可以使用我的數據庫中的sqllite搜索它所屬的位置,獲取經度和緯度,並告知mapcontroller關注這一點的世界地圖。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.verladestellen); 
    final DB_Verladestellen db = new DB_Verladestellen(this); 
    List<DB_Place> placeList = db.getAllDBPlaces(); 
    final LinearLayout layout = (LinearLayout) findViewById(R.id.verladestellen_liste); 

    for (final DB_Place cn : placeList) { 

     final LinearLayout row = new LinearLayout(this); 
     row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

     Button place = new Button(this); 
     place.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

     place.setText(cn.getName()); 
     place.setId(cn.getID()); 
     row.addView(place); 
     row.setId(cn.getID()); 

     LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) place.getLayoutParams(); 
     params.weight = 1.0f; 
     place.setLayoutParams(params); 

     place.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Here I want to call the method to start the other activity 
       //and transmit cn.getID(). 
       openMap(null); 
      } 
     }); 

     layout.addView(row); 
    } 
} 

//The method to start the other activity 
public void openMap(View view) { 
    Intent intent = new Intent(this, UI_MainActivity.class); 
    startActivity(intent); 
} 

這是新的活動我想immidietaly執行裏面的方法,它已開始之後:

public void findLocation(View view){ 
    MapView map = (MapView) findViewById(R.id.map); 
    IMapController mapController = map.getController(); 
    mapController.setZoom(17); 
    GeoPoint myLocation = new GeoPoint(PLACEHOLDER X , PLACEHOLDER Y); 
    mapController.animateTo(myLocation); 
} 

編輯: @Murat K.一些修改後,這是我現在全班同學:

public class UI_Verladestellen extends AppCompatActivity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.verladestellen); 
    final DB_Verladestellen db = new DB_Verladestellen(this); 
    List<DB_Place> placeList = db.getAllDBPlaces(); 
    final LinearLayout layout = (LinearLayout) findViewById(R.id.verladestellen_liste); 

    for (final DB_Place cn : placeList) { 

     final LinearLayout row = new LinearLayout(this); 
     row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

     Button place = new Button(this); 
     place.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

     place.setText(cn.getName()); 
     place.setId(cn.getID()); 
     row.addView(place); 
     row.setId(cn.getID()); 

     LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) place.getLayoutParams(); 
     params.weight = 1.0f; 
     place.setLayoutParams(params); 

     place.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       openMap(cn.getID()); 
      } 
     }); 

     layout.addView(row); 
    } 
} 

public void openMap(int view) { 
    Intent intent = new Intent(UI_Verladestellen.this, UI_MainActivity.class); 
    intent.putExtra("findLocation", 1); 
    startActivity(intent); 
} 

}

這是我的onCreate的getIntent方法在UI_MainActivity:

int i = getIntent().getIntExtra("findlocation", 999); 
    if(i == 1){ 
     findLocation(i); 
    } 

當我編輯到我以前的評論,我不能看到我的按鈕ID收到的地方。起初我以爲我會是我的ID,但那不起作用,因爲按鈕ID可以是從1到n的每個數字。

+0

在你的'openMap()'方法中,你沒有把任何東西放入Intent中。您必須完全按照原樣使用我的答案。 –

+0

@MuratK。我以爲我的確很好地回答了你的答案。也許我誤解了你。那麼你能告訴我我錯過了什麼嗎?對不起,我不是很有經驗。 – Glave

回答

1

您可以通過例如Intent來實現此目的。

Intent i = new Intent(BaseActivity.this, YourSecondActivity.class); 
intent.putExtra("METHOD_TO_CALL", 1); 
startActivity(i); 

,並在您開始Activity你檢查它的onCreate()方法。

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    int i = getIntent().getIntExtra("METHOD_TO_CALL", 999); 
    if(i == 1){ 
     callMethod(i); 
    } 

編輯:

//The method to start the other activity 
public void openMap(View view) { 
    Intent intent = new Intent(this, UI_MainActivity.class); 
    intent.putExtra("METHOD_TO_CALL", 1); // the 1 is a example, put your ID here 
    startActivity(intent); 
} 
+0

某些東西不能正常工作。這是如何理解它:''intent.putExtra(「findLocation」,1)'和'int i = getIntent()。getIntExtra(「findlocation」,999)!= 999;'在Android Studio告訴我的最後一行「不兼容的類型。Required:Int,Found:Boolean「。 – Glave

+0

@Glave oooops,刪除'!='檢查。我原本打算創建一個if語句。 –

+0

好吧,活動現在開始了,但還是不對。我的'setOnClickListener'現在包含這個代碼:'openMap(cn.getID());',但是當我點擊按鈕時,新的活動告訴我'i'是999(這個數字有特殊原因嗎?)而不是1,並且不調用該方法。也。我現在看不到,現在我的按鈕ID在新的活動中被收到。如果我'我== 1'它不能是我的按鈕ID。 – Glave

1

第1步:使用putExtra()到您的ID添加到您startActivity()

第2步使用Intent:在其他活動,稱之爲getIntent()onCreate()中檢索用於創建活動實例的Intent。請致電get...Extra()(其中...取決於數據類型)來檢索您的ID值。如果ID值存在,請致電findLocation()方法。

0

這取決於你希望如何工作。如果您只希望在創建活動時(啓動時或屏幕旋轉時)執行它,請在方法onCreate活動內調用該方法。如果您希望每當用戶返回到可以包括他們離開應用程序並返回該應用程序的活動時調用此應用程序,那麼onResume將是更好的選擇。從任何一方調用該方法都應該如你所希望的那樣工作。

我還建議查看活動生命週期,因爲這將在未來幫助您很多。

相關問題