コード例 #1
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;
  }