安卓:通过片段之间一个JSONObject [英] Android: pass a JSONobject between Fragments
问题描述
我正在它采用了ListFragment以显示每一天的天气和更多信息的细节片段一点点天气应用程序。我试图让每一个细节片段显示每个列表视图一天相应的天气。以下是code的一些片段:
在ListFragment的部分:
公共类WeatherListFragment扩展ListFragment实现LocationListener的{ 私人最终字符串initialURL =https://api.forecast.io/forecast/8fc2b0556e166fa4670d4014d318152a/;
天气[] myWeatherArray = {};
WeatherAdapter weatherAdapter;
的LocationManager mLocationManager;
串currentLoc;
的JSONObject日; OnWeatherSelectedListener mCallback; // Container活动必须实现此接口使FRAG可以传递消息
公共接口OnWeatherSelectedListener {
通过调用HeadlinesFragment / **当选择列表项* /
公共无效onWeatherSelected(INT位置,字符串数据);
}@覆盖
公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){ //与所选项目回调父活动 mCallback.onWeatherSelected(位置,数据);
l.setItemChecked(位置,真正的);
}
尝试{
每天的JSONObject = response.getJSONObject(日报);
JSONArray数据= daily.getJSONArray(数据);
myWeatherArray =新气象[data.length()];
的for(int i = 0; I< myWeatherArray.length;我++){
天= data.getJSONObject(ⅰ);
天气myWeatherObject =新气象();
myWeatherObject.setmDate(day.getInt(时代));
myWeatherObject.setmTempMin(day.getInt(temperatureMin));
myWeatherObject.setmTempMax(day.getInt(temperatureMax));
myWeatherObject.setIcon(day.getString(图标));
myWeatherArray [I] = myWeatherObject; } }赶上(JSONException E){
e.printStackTrace();}
主要活动:
公共类MainActivity扩展ActionBarActivity实现WeatherListFragment.OnWeatherSelectedListener { @覆盖
公共无效onWeatherSelected(INT位置,字符串数据){
//用户选择的文章的标题从HeadlinesFragment
WeatherDetailFragment weatherDetailFragment =(WeatherDetailFragment)getSupportFragmentManager()findFragmentById(R.id.weather_detail_fragment)。 //一个面板布局
如果(weatherDetailFragment == NULL){
WeatherDetailFragment onePaneFragment =新WeatherDetailFragment(); 捆绑ARGS =新包();
args.putInt(WeatherDetailFragment.ARG_POSITION,位置); onePaneFragment.setArguments(参数); getSupportFragmentManager()调用BeginTransaction()
.replace(R.id.container,onePaneFragment)
.addToBackStack(空)
。承诺();
细节片段:
公共类WeatherDetailFragment扩展片段{ 最终静态字符串ARG_POSITION =位置; INT mCurrentPosition = -1;
。
。
。
公共无效updateWeatherView(INT位置,字符串数据){ 视图V = getView();
TextView的天=(TextView中)v.findViewById(R.id.dayTextView);
TextView的日期=(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 =位置;
}
。
。
}
传递的JSONObject
如果您使用可以更容易的捆绑
对象。转换的JSONObject
到字符串
由JSONObject.toString()方法。然后将其设置为捆绑
对象。例如:
捆绑ARGS =新包();
串userProfileString = userProfileJsonObject.toString();
args.putString(userProfileString,userProfileString);
fragmentUserProfile.setArguments(参数);
然后在目标片段中,onCreateView方法中,你可以提取字符串,并将其重新转换到JSONObject的。
字符串userProfileString = getArguments()的getString(userProfileString);
的JSONObject的JSONObject =新的JSONObject(userProfileString);
希望这有助于。
I'm working on a little Weather app which uses a ListFragment to show the weather for each day and a detail fragment with more information. I'm trying to get each detail fragment to show the corresponding weather for each listview day. Here are some snippets of code:
Parts of the 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();
}
The main 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();
The detail 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;
}
.
.
}
Passing JSONObject
can be easier if you use the Bundle
object. Convert the JSONObject
to a String
by the JSONObject.toString() method. Then set it to the Bundle
object. For example:
Bundle args=new Bundle();
String userProfileString=userProfileJsonObject.toString();
args.putString("userProfileString", userProfileString);
fragmentUserProfile.setArguments(args);
Then in the destination fragment, inside the onCreateView method you can extract the String and reconvert it to the JSONObject.
String userProfileString=getArguments().getString("userProfileString");
JSONObject jsonObject=new JSONObject(userProfileString);
Hope this helps.
这篇关于安卓:通过片段之间一个JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!