2013-08-19 4 views
3

하나의 액티비티에서 다른 액티비티로 설정 한 문자열 (locationSet)을 각 액티비티에 Asynchtask로 구현하려고합니다.Android Asynctasks, 액티비티간에 문자열 전달

MainActivity의 값을 WeatherActivity로 전달할 수 없습니다. 로그를 사용하여 MainActivity에서 초기 값이 잘 설정되어 있음을 알 수 있지만 Weather Activity로 전달하려고 할 때 null입니다. 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); 
    } 
} 

}

당신은 Intent.putExtra ("TAG"를 사용하여 문자열 값 "locationSet"를 전달할 수 있습니다

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) '에 잊어 버렸습니다. 당신이 의도 한 'WeatherActivity'를 가져 와서 보낸 동일한 ID로 문자열을 검색하십시오.이 경우에는'STRING_I_NEED'입니다. 희망이 도움이됩니다. – user2652394

+1

MainActivity에서 asynctask를 사용할 필요가 없습니다. 가치를 의도에 넣고 WeatherActivity로 되돌립니다. –

+0

응답 해 주셔서 감사합니다. 아래에서 말씀 드렸듯이 모든 것이 해결되지는 않았지만 해결 방법이 한 가지 개선되었습니다. – TheBluePotter

답변

2

WeatherActivity.java , locationSet). onCreate의 날씨 활동에서 Intent.getExtra를 사용하여 값을 얻을 수 있습니다.

코드 예제 :, 당신의`WeatherActivity`로`MainActivity`에서`

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에 null이있는 문자열 (위치를 나타내는)이 있어야한다는 것을 곧바로 볼 수 있습니다. WeatherActivity에서 – TheBluePotter

관련 문제