コード例 #1
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String location =
        sharedPref.getString(
            getString(R.string.pref_location_key), getString(R.string.pref_location_default));
    String unit =
        sharedPref.getString(
            getString(R.string.pref_units_key), getString(R.string.pref_units_imperial));
    Intent intent = null;

    // Handle item selection
    switch (item.getItemId()) {
      case R.id.action_refresh:
        FetchWeatherTask task = new FetchWeatherTask();
        task.execute(location, unit);
        return true;
      case R.id.action_map_location:
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("geo:0,0?q=" + location));
        if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
          startActivity(intent);
        }
        return true;
      case R.id.action_settings:
        intent = new Intent(getActivity(), SettingsActivity.class);
        startActivity(intent);
        return super.onOptionsItemSelected(item);
      default:
        return super.onOptionsItemSelected(item);
    }
  }
コード例 #2
0
 private void updateWeather() {
   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
   String location =
       prefs.getString(
           getString(R.string.pref_location_key), getString(R.string.pref_default_value));
   FetchWeatherTask fetcher = new FetchWeatherTask();
   fetcher.execute(location);
 }
コード例 #3
0
 private void updateWeather() {
   FetchWeatherTask task = new FetchWeatherTask();
   SharedPreferences sharedPreferences =
       PreferenceManager.getDefaultSharedPreferences(getActivity());
   String location =
       sharedPreferences.getString(
           getString(R.string.pref_key_location), getString(R.string.pref_default_value));
   task.execute(location);
   // task.execute("Jakarta");
 }
コード例 #4
0
 private void updateWeather() {
   FetchWeatherTask fetchWeatherTask = new FetchWeatherTask();
   SharedPreferences sharedPreferences =
       PreferenceManager.getDefaultSharedPreferences(getActivity());
   String string;
   string =
       sharedPreferences.getString(
           getString(R.string.pref_location_key), getString(R.string.pref_location_default));
   fetchWeatherTask.execute(string);
 }
コード例 #5
0
  private void RefreshData() {
    SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
    String location =
        prefs.getString(
            getString(R.string.location_key), getString(R.string.default_label_location));
    String units =
        prefs.getString(getString(R.string.pref_units_key), getString(R.string.pref_units_metric));

    FetchWeatherTask weatherTask = new FetchWeatherTask();
    String[] params = {location, units};
    weatherTask.execute(params);
  }
コード例 #6
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_refresh) {

      FetchWeatherTask task = new FetchWeatherTask();
      task.execute("94043");
      return true;
    }

    return super.onOptionsItemSelected(item);
  }
コード例 #7
0
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    setHasOptionsMenu(true);

    FetchWeatherTask task = new FetchWeatherTask();
    String[] weatherData = new String[0];
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String location =
        sharedPref.getString(
            getString(R.string.pref_location_key), getString(R.string.pref_location_default));
    String unit =
        sharedPref.getString(
            getString(R.string.pref_units_key), getString(R.string.pref_units_imperial));
    task.execute(location, unit);
    try {
      weatherData = task.get();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      e.printStackTrace();
    }

    final ArrayAdapter<String> myAdapter =
        new ArrayAdapter<String>(
            getActivity(),
            R.layout.list_item_forecast,
            R.id.list_item_forecast_textview,
            weatherData);

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
    listView.setAdapter(myAdapter);
    listView.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Intent intent = new Intent(getActivity(), DetailActivity.class);
            intent.putExtra(Intent.EXTRA_TEXT, myAdapter.getItem(position));
            startActivity(intent);
          }
        });

    return rootView;
  }
コード例 #8
0
 private void updateWeather() {
   try {
     //                String[] forecasts = weatherTask.execute("94043").get();
     //                _weekForecast = Arrays.asList( forecasts);
     //                bindData(_rootView);
     //                Log.v("DATA", "here");
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
     String location =
         prefs.getString(
             getString(R.string.pref_location_key),
             getString(R.string.pref_location_defaultValue));
     FetchWeatherTask weatherTask = new FetchWeatherTask();
     weatherTask.execute(location);
   } catch (Exception ex) {
     Log.e(this.getClass().getName(), ex.getMessage());
   }
 }
コード例 #9
0
  /*
     Students: uncomment testAddLocation after you have written the AddLocation function.
     This test will only run on API level 11 and higher because of a requirement in the
     content provider.
  */
  @TargetApi(11)
  public void testAddLocation() {
    // start from a clean state
    getContext()
        .getContentResolver()
        .delete(
            WeatherContract.LocationEntry.CONTENT_URI,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
            new String[] {ADD_LOCATION_SETTING});

    FetchWeatherTask fwt = new FetchWeatherTask(getContext());
    long locationId =
        fwt.addLocation(
            ADD_LOCATION_SETTING, ADD_LOCATION_CITY, ADD_LOCATION_LAT, ADD_LOCATION_LON);

    // does addLocation return a valid record ID?
    assertFalse("Error: addLocation returned an invalid ID on insert", locationId == -1);

    // test all this twice
    for (int i = 0; i < 2; i++) {

      // does the ID point to our location?
      Cursor locationCursor =
          getContext()
              .getContentResolver()
              .query(
                  WeatherContract.LocationEntry.CONTENT_URI,
                  new String[] {
                    WeatherContract.LocationEntry._ID,
                    WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING,
                    WeatherContract.LocationEntry.COLUMN_CITY_NAME,
                    WeatherContract.LocationEntry.COLUMN_COORD_LAT,
                    WeatherContract.LocationEntry.COLUMN_COORD_LONG
                  },
                  WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
                  new String[] {ADD_LOCATION_SETTING},
                  null);

      // these match the indices of the projection
      if (locationCursor.moveToFirst()) {
        assertEquals(
            "Error: the queried value of locationId does not match the returned value"
                + "from addLocation",
            locationCursor.getLong(0),
            locationId);
        assertEquals(
            "Error: the queried value of location setting is incorrect",
            locationCursor.getString(1),
            ADD_LOCATION_SETTING);
        assertEquals(
            "Error: the queried value of location city is incorrect",
            locationCursor.getString(2),
            ADD_LOCATION_CITY);
        assertEquals(
            "Error: the queried value of latitude is incorrect",
            locationCursor.getDouble(3),
            ADD_LOCATION_LAT);
        assertEquals(
            "Error: the queried value of longitude is incorrect",
            locationCursor.getDouble(4),
            ADD_LOCATION_LON);
      } else {
        fail("Error: the id you used to query returned an empty cursor");
      }

      // there should be no more records
      assertFalse(
          "Error: there should be only one record returned from a location query",
          locationCursor.moveToNext());

      // add the location again
      long newLocationId =
          fwt.addLocation(
              ADD_LOCATION_SETTING, ADD_LOCATION_CITY, ADD_LOCATION_LAT, ADD_LOCATION_LON);

      assertEquals(
          "Error: inserting a location again should return the same ID", locationId, newLocationId);
    }
    // reset our state back to normal
    getContext()
        .getContentResolver()
        .delete(
            WeatherContract.LocationEntry.CONTENT_URI,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
            new String[] {ADD_LOCATION_SETTING});

    // clean up the test so that other tests can use the content provider
    getContext()
        .getContentResolver()
        .acquireContentProviderClient(WeatherContract.LocationEntry.CONTENT_URI)
        .getLocalContentProvider()
        .shutdown();
  }
コード例 #10
0
 private void updateWeather() {
   FetchWeatherTask weatherTask = new FetchWeatherTask(getActivity());
   String location = Utility.getPreferredLocation(getActivity());
   weatherTask.execute(location);
 }