Пример #1
0
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // The CursorAdapter will take data from our cursor and populate the ListView.
    mForecastAdapter = new ForecastAdapter(getActivity(), null, 0);
    mForecastAdapter.setmUseTodayLayout(mUseTodayLayout);
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    // Get a reference to the ListView, and attach this adapter to it.
    mListView = (ListView) rootView.findViewById(R.id.listview_forecast);
    mListView.setAdapter(mForecastAdapter);

    // We'll call our MainActivity
    mListView.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // CursorAdapter returns a cursor at the correct position for getItem(), or null
            // if it cannot seek to that position.
            Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
            if (cursor != null) {
              String locationSetting = Utility.getPreferredLocation(getActivity());
              ((Callback) getActivity())
                  .onItemSelected(
                      WeatherContract.WeatherEntry.buildWeatherLocationWithDate(
                          locationSetting, cursor.getLong(COL_WEATHER_DATE)));
              mPosition = position;
            }
          }
        });

    if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) {
      mPosition = savedInstanceState.getInt(SELECTED_KEY);
    }

    mForecastAdapter.setmUseTodayLayout(mUseTodayLayout);

    return rootView;
  }
Пример #2
0
 public void setUseTodayLayout(boolean useTodayLayout) {
   mUseTodayLayout = useTodayLayout;
   if (mForecastAdapter != null) {
     mForecastAdapter.setmUseTodayLayout(mUseTodayLayout);
   }
 }