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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With