0

我灣來隱式地傳遞兩個字符串或變量從dialoguebox到由另一活動,並在同一時間打開該活動中使用。傳遞一個字符串或一變量從dialoguebox到活動的Android

dialoguebox

String city = addresses.get(0).getLocality(); 
String category = "Hazard"; 

      AlertDialog.Builder prompt = new 
      AlertDialog.Builder(MapsActivity.this); 
      prompt.setCancelable(false) 
        .setPositiveButton("Mineralization", new 
      DialogInterface.OnClickListener() { 

        .setPositivetiveButton("Geohazard", new 
          DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, 
           int which) { 

            startActivity(new 
         Intent(getApplicationContext(),MapsActivity.class)); 

         //i want to pass the String city and category here 
           } 
          } 
        ) 





      AlertDialog alert = prompt.create(); 
      alert.setTitle("Please select an option"); 
      alert.show(); 

      TextView myTextView = (TextView) findViewById(R.id.PSString); 
      myTextView.setText(city); 

新活動

package com.example.boneyflesh.homepage; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class GeohazardResults extends AppCompatActivity { 

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

我如何做到這一點?我該寫在onClickListener什麼,我該怎麼辦就新的活動,這樣我可以得到的字符串?

回答

0

而是在你的onClick方法做這樣的事情的

startActivity(new Intent(getApplicationContext(),MapsActivity.class)); 

您只需通過意圖您的onClick方法裏面MapsActivity傳遞兩個字符串如下圖所示

Intent lIntent = new Intent(getApplicationContext(), MapsActivity.class); 
lIntent.putExtra("city", city); 
lIntent.putExtra("category", category); 
startActivity(lIntent); 

和檢索MapsActivity中的那些值如下所示

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_geohazard_results); 
    String city = getIntent().getStringExtra("city"); 
    String category = getIntent().getStringExtra("category"); 
} 
+3

很好,謝謝你。其他答案也可以工作,但我選擇了這個答案,因爲對於像我這樣的初學者來說,它是明確而簡單易懂的。 :豎起大拇指: – Boneyflesh

+0

@Boneyflesh你應該考慮提供有用的答案(除了選擇「獲勝者」) – theblitz

+1

@theblitz我確實誇大了他們,但它表示一旦我的聲望超過15 T_T,公共狀態將會改變。 – Boneyflesh

0

而不是做這個的 - :

startActivity(new Intent(getApplicationContext(),MapsActivity.class)); 

做這樣的事情 - :

Intent mapsActivityIntent = new Intent(getApplicationContext(), MapsActivity.class); 
mapsActivityIntent.putExtra("CITY", city); 
mapsActivityIntent.putExtra("CATEGORY", category); 
startActivity(mapsActivityIntent); 

在你的活動,你可以那麼做讓他們:

getIntent().getStringExtra("CITY"); 
getIntent().getStringExtra("CATEGORY"); 
0

通行證使用意圖和捆綁像下面:

Intent intent = new Intent(getApplicationContext(),MapsActivity.class)); 
Bundle bundle =new Bundle(); 
bundle.putString("key1","value1"); 
bundle.putString("key2","value2"); 
intent.putExtras(bundle); 
startActivity(intent); 

並檢索下一個活動中的值,如下所示:

Bundle bundle = getIntent().getExtras(); 
String value1= bundle.getString("key1"); 
String value2= bundle.getString("key2"); 
0

您需要使用額外功能來發送數據。

例如:

Intent intent = new Intent(getApplicationContext(),MapsActivity.class); 
intent.putExtra("SOME_NAME_HERE", theString); 
startActivity(intent); 

然後,在目標活動你寫的 - 在的onCreate:

Bundle extras = getIntent().getExtras(); 
if(extras == null) { 
    theString= null; 
} else { 
    theString = extras.getString("SOME_NAME_HERE"); 
} 
+0

謝謝,這也適用 – Boneyflesh

0

在您的對話框:

Intent i = new Intent(MapsActivity.this, GeohazardResults.class); 
i.putExtra("city", city); 
i.putExtra("category", categoty); 
startActivity(i); 

在新的活動:

Intent i = getIntent(); 
Bundle extras = i.getExtras(); 
String city = extras.getString("city"); 
String category = extras.getString("category"); 
0
DialogInterface.OnClickListener() { 
       .setPositivetiveButton("Geohazard", new 
         DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, 
          int which) { 
          Intent intent = new Intent(getApplicationContext(),MapsActivity.class)) 
          // intent.putExtra(key,value); 
          intent.putExtra("String1","value1"); 
          intent.putExtra("String2","value2"); 

          startActitvity(intent); 
          } 
         } 
       ) 
在Acivity

  Intent intent = getIntent(); 
      Bundle extras = intent.getExtras(); 
      if(extras != null){ 
      //intent.getStringExtra(key) 
      string1 = intent.getStringExtra("String1"); 
      string2 = intent.getStringExtra("String2"); 
      } 

我建議定義按鍵爲常數,並使用這些同時傳遞價值,以避免任何混淆

0

傳球意圖從您的對話框這樣

Intent intent=new Intent(context, GeohazardResults.class); 
     Bundle parameters=new Bundle(); 
     parameters.putString("city",city); 
     parameters.putString("category",category); 
     intent.putExtra("key",parameters); 
     startActivity(intent); 

,並在接下來的活動接收這樣的thiese值。

Intent receivedIntent=getIntent(); 
     if(receivedIntent!=null){ 
      Bundle parameters=receivedIntent.getBundleExtra("key"); 
      if(parameters!=null){ 
       String city=parameters.getString("city"); 
       String category=parameters.getString("category"); 
      } 
     }