2013-08-19 37 views
3

我想傳遞一個字符串(locationSet)由一個微調從一個活動設置到另一個,他們都在每個類中實現一個Asynchtask。Android Asynctasks,在活動之間傳遞字符串

我無法將MainActivity的值傳遞給我的WeatherActivity。使用日誌,我可以看到MainActivity中的初始值設置正確,但我嘗試將其傳遞給天氣活動時無效。如果我手動輸入WeatherActivity中的相關字符串,則輸出按預期工作。

我看過其他問題,但我發現很難應用任何我看到的。

MainActivity.java

public class MainActivity extends Activity { 

Button searchLocation; 
Spinner locationSpinner; 
String locationSet = null; 
String strLocation = null; 
Locations arrLocation; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    searchLocation = (Button) findViewById(R.id.searchLocationButton); 

    locationSpinner = (Spinner) findViewById(R.id.locationsListView); 

    ArrayAdapter<CharSequence> adaptor = ArrayAdapter.createFromResource(this, R.array.location_array, android.R.layout.simple_list_item_1); 
    locationSpinner.setAdapter(adaptor); 

    searchLocation.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      new MyAsync().execute(); 

     } 
    }); 
} 

public void onBackPressed() { 
    super.onBackPressed(); 
    this.finish(); 
} 

class MyAsync extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 

     strLocation = locationSpinner.getSelectedItem().toString(); 

     if (strLocation.equals("Aberdeen")) { 
      locationSet = arrLocation.ABERDEEN; 
     } else if (strLocation.equals("Dundee")) { 
      locationSet = arrLocation.DUNDEE; 
     } else if (strLocation.equals("Edinburgh")) { 
      locationSet = arrLocation.EDINBURGH; 
     } else if (strLocation.equals("Fort William")) { 
      locationSet = arrLocation.FORT_WILLIAM; 
     } else if (strLocation.equals("Glasgow")) { 
      locationSet = arrLocation.GLASGOW; 
     } else if (strLocation.equals("Manchester")) { 
      locationSet = arrLocation.MANCHESTER; 
     } else if (strLocation.equals("North Berwick")) { 
      locationSet = arrLocation.NORTH_BERWICK; 
     } else if (strLocation.equals("Portree")) { 
      locationSet = arrLocation.PORTREE; 
     } else if (strLocation.equals("Ullapool")) { 
      locationSet = arrLocation.ULLAPOOL; 
     } 

     Intent intent = new Intent(MainActivity.this, WeatherActivity.class); 
     startActivity(intent); 
    } 
} 

}

WeatherActivity.java

public class WeatherActivity extends MainActivity { 

    TextView firstDayTextView; 

    String selectedLocation = null; 

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

     locationTextView = (TextView) findViewById(R.id.locationTextView); 



     new LoginAsync().execute(); 
    } 

    class LoginAsync extends AsyncTask<Void, Void, Void> { 
     MySaxHandler myHandler; 

     StringBuilder builder = new StringBuilder(); 

     @Override 
     protected Void doInBackground(Void... params) { 
      myHandler = new MySaxHandler(); 
      myHandler.addLocation(selectedLocation.toString()); 
      myHandler.get(); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 

      ArrayList<ItemData> items = myHandler.items; 

      if (null != items && items.size() != 0) { 
       for (int index = 0; index < items.size(); index++) { 
        ItemData obJPost = items.get(index); 
         builder.append("\n" + obJPost.getItemTitle()); 
       } 
      } 
      firstDayTextView.setText(builder.toString()); 

     } 
    } 
} 
+1

你忘了'intent.putExtra( 「STRING_I_NEED」,locationSet);'從'MainActivity'你'WeatherActivity'。確保你獲得了'WeatherActivity'的意圖表單,並檢索你發送的相同ID的字符串,在這種情況下是'STRING_I_NEED'。希望這可以幫助。 – user2652394

+1

不需要在MainActivity中使用asynctask。只需將價值放在意向中,並將其返回到WeatherActivity中即可。 –

+0

感謝您的迴應,我在下面說過,解決方案改進了一件事,但沒有解決所有問題。 – TheBluePotter

回答

2

你可以通過你的字符串值 「locationSet」 使用Intent.putExtra( 「TAG」 ,locationSet),同時開始你的天氣活動。在onCreate的天氣活動中,您可以使用Intent.getExtra獲取該值。

代碼示例:

Intent i = new Intent(FirstScreen.this, SecondScreen.class); 
String keyIdentifer = null; 
i.putExtra("STRING_I_NEED", strName); 

Then, to retrieve the value try something like: 
String newString; 
if (savedInstanceState == null) { 
    extras = getIntent().getExtras(); 
    if(extras == null) { 
     newString= null; 
    } else { 
     newString= extras.getString("STRING_I_NEED"); 
    } 
} else { 
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED"); 
} 
+0

感謝您的回覆,現在我在WeatherActivy中的日誌顯示了一個值,但是我爲什麼在切換活動時崩潰感到困惑。 我很困惑,我可能在我的代碼中有什麼錯誤? – TheBluePotter

+0

你可以在這裏添加崩潰日誌嗎?或者你可以告訴它在哪一行崩潰? – Sushil

+0

我已經在上面添加了日誌的副本,我希望這是足夠的信息。我可以直接看到URL應該有一個空字符串(表示一個位置)。在WeatherActivity中使用 – TheBluePotter