[英]How do I pass gson JsonObject between fragments in the same activity
[英]Android: pass a JSONobject between Fragments
我正在開發一個小天氣應用程序,它使用ListFragment來顯示每天的天氣以及包含更多信息的詳細信息片段。 我正在嘗試獲取每個詳細信息片段以顯示每個列表視圖日的相應天氣。 以下是一些代碼片段:
ListFragment的部分內容:
public class WeatherListFragment extends ListFragment implements LocationListener{
private final String initialURL = "https://api.forecast.io/forecast/8fc2b0556e166fa4670d4014d318152a/";
Weather[] myWeatherArray = {};
WeatherAdapter weatherAdapter;
LocationManager mLocationManager;
String currentLoc;
JSONObject day;
OnWeatherSelectedListener mCallback;
// The container Activity must implement this interface so the frag can deliver messages
public interface OnWeatherSelectedListener {
/** Called by HeadlinesFragment when a list item is selected */
public void onWeatherSelected(int position, String data);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//call back to the parent activity with the selected item
mCallback.onWeatherSelected(position, data);
l.setItemChecked(position, true);
}
try {
JSONObject daily = response.getJSONObject("daily");
JSONArray data = daily.getJSONArray("data");
myWeatherArray = new Weather[data.length()];
for (int i = 0; i < myWeatherArray.length; i++) {
day = data.getJSONObject(i);
Weather myWeatherObject = new Weather();
myWeatherObject.setmDate(day.getInt("time"));
myWeatherObject.setmTempMin(day.getInt("temperatureMin"));
myWeatherObject.setmTempMax(day.getInt("temperatureMax"));
myWeatherObject.setIcon(day.getString("icon"));
myWeatherArray[i] = myWeatherObject;
}
} catch (JSONException e) {
e.printStackTrace();
}
主要活動:
public class MainActivity extends ActionBarActivity implements WeatherListFragment.OnWeatherSelectedListener {
@Override
public void onWeatherSelected(int position, String data) {
// The user selected the headline of an article from the HeadlinesFragment
WeatherDetailFragment weatherDetailFragment = (WeatherDetailFragment)getSupportFragmentManager().findFragmentById(R.id.weather_detail_fragment);
//One pane layout
if(weatherDetailFragment == null) {
WeatherDetailFragment onePaneFragment = new WeatherDetailFragment();
Bundle args = new Bundle();
args.putInt(WeatherDetailFragment.ARG_POSITION, position);
onePaneFragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, onePaneFragment)
.addToBackStack(null)
.commit();
細節片段:
public class WeatherDetailFragment extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
.
.
.
public void updateWeatherView(int position, String data) {
View v = getView();
TextView day = (TextView) v.findViewById(R.id.dayTextView);
TextView date = (TextView) v.findViewById(R.id.dateTextView);
TextView tempMin = (TextView) v.findViewById(R.id.tempMinTextView);
TextView tempMinF = (TextView) v.findViewById(R.id.tempMinFTextView);
TextView tempMax = (TextView) v.findViewById(R.id.tempMaxTextView);
TextView tempMaxF = (TextView) v.findViewById(R.id.tempMaxFTextView);
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
ImageView imageView2 = (ImageView) v.findViewById(R.id.imageView2);
mCurrentPosition = position;
}
.
.
}
如果使用Bundle
對象,則傳遞JSONObject
會更容易。 通過JSONObject.toString()方法將JSONObject
轉換為String
。 然后將其設置為Bundle
對象。 例如:
Bundle args=new Bundle();
String userProfileString=userProfileJsonObject.toString();
args.putString("userProfileString", userProfileString);
fragmentUserProfile.setArguments(args);
然后在目標片段中,在onCreateView方法內部,您可以提取String並將其重新轉換為JSONObject。
String userProfileString=getArguments().getString("userProfileString");
JSONObject jsonObject=new JSONObject(userProfileString);
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.