gpt4 book ai didi

Android:在 fragment 之间传递一个 JSONobject

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:11 26 4
gpt4 key购买 nike

我正在开发一个小天气应用程序,它使用 ListFragment 来显示每天的天气和包含更多信息的详细信息 fragment 。我试图让每个细节 fragment 显示每个 ListView 天的相应天气。以下是一些代码 fragment :

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();

}

主要 Activity :

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();

详细 fragment :

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);

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

String userProfileString=getArguments().getString("userProfileString");
JSONObject jsonObject=new JSONObject(userProfileString);

希望这对您有所帮助。

关于Android:在 fragment 之间传递一个 JSONobject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20402720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com