Android:在Fragment之间传递JSON对象

5

我正在开发一个小型天气应用程序,它使用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;
    }
.
.
}

使用接口作为回调。所有片段到片段的通信都通过相关联的活动完成。两个片段永远不应直接通信。http://developer.android.com/training/basics/fragments/communicating.html - Raghunandan
抱歉,我知道这个。我正在使用回调将ListView位置从ListFragment传递到Activity,然后再传递给详细信息Fragment。我不确定如何将每天的JSONObject转换为字符串以便传递回去。 - fiixed
绑定到HashMap对象类并将其发送到其他片段怎么样! - Thein
1个回答

16

如果您使用Bundle对象,则传递JSONObject可能更容易。通过调用 JSONObject.toString() 方法将JSONObject转换为String。然后将其设置到Bundle对象中。例如:

   Bundle args=new Bundle();
   String userProfileString=userProfileJsonObject.toString();
   args.putString("userProfileString", userProfileString);
   fragmentUserProfile.setArguments(args);

然后,在目标片段中,在onCreateView方法内,您可以提取该字符串并将其重新转换为JSONObject。

String userProfileString=getArguments().getString("userProfileString");
JSONObject jsonObject=new JSONObject(userProfileString);
希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接