하나의 액티비티에서 다른 액티비티로 설정 한 문자열 (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());
}
}
}
당신은 intent.putExtra ("STRING_I_NEED", locationSet) '에 잊어 버렸습니다. 당신이 의도 한 'WeatherActivity'를 가져 와서 보낸 동일한 ID로 문자열을 검색하십시오.이 경우에는'STRING_I_NEED'입니다. 희망이 도움이됩니다. – user2652394
MainActivity에서 asynctask를 사용할 필요가 없습니다. 가치를 의도에 넣고 WeatherActivity로 되돌립니다. –
응답 해 주셔서 감사합니다. 아래에서 말씀 드렸듯이 모든 것이 해결되지는 않았지만 해결 방법이 한 가지 개선되었습니다. – TheBluePotter