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