fii*_*xed 4 android json android-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();
}
Run Code Online (Sandbox Code Playgroud)
主要活动:
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();
Run Code Online (Sandbox Code Playgroud)
细节片段:
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;
}
.
.
}
Run Code Online (Sandbox Code Playgroud)
Md.*_*mud 14
JSONObject如果使用该Bundle对象,传递可以更容易.通过JSONObject.toString()方法将其转换JSONObject为a String.然后将其设置为Bundle对象.例如:
Bundle args=new Bundle();
String userProfileString=userProfileJsonObject.toString();
args.putString("userProfileString", userProfileString);
fragmentUserProfile.setArguments(args);
Run Code Online (Sandbox Code Playgroud)
然后在目标片段中,在onCreateView方法内部,您可以提取String并将其重新转换为JSONObject.
String userProfileString=getArguments().getString("userProfileString");
JSONObject jsonObject=new JSONObject(userProfileString);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
6086 次 |
| 最近记录: |