Android Open Source - CoolWeather Search






From Project

Back to project page CoolWeather.

License

The source code is released under:

GNU General Public License

If you think the Android project CoolWeather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.das.coolweather.fragments;
//w  w w.  j ava 2 s .  c  om
import org.das.coolweather.R;
import org.das.coolweather.activities.DetailsActivityHost;
import org.das.coolweather.utils.WeatherHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.TextView;

public class Search extends Fragment  {

  private TextView txtSearchCity, txtResultTempMin, txtResultTempMax, txtTempMin, txtTempMax;
  private ImageButton btnSearchTerm;
  private JSONObject cityData;
  
  public static Search newInstance() {
    Search fragment = new Search();
    return fragment;
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.search, container,
        false);
    
    txtSearchCity = (TextView) rootView.findViewById(R.id.txtSearchCity);
    txtResultTempMin = (TextView) rootView.findViewById(R.id.txtResultTempMin);
    txtResultTempMax = (TextView) rootView.findViewById(R.id.txtResultTempMax);
    txtTempMin = (TextView) rootView.findViewById(R.id.txtSearchTempMin);
    txtTempMin.setTextSize(20);
    txtTempMax = (TextView) rootView.findViewById(R.id.txtSearchTempMax);
    txtTempMax.setTextSize(20);
    btnSearchTerm = (ImageButton) rootView.findViewById(R.id.btnSearchDetails);
    
    btnSearchTerm.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
        Intent intentDetails = new Intent(getActivity(), DetailsActivityHost.class);
        if(cityData == null){
          Toast.makeText(getActivity(), "Busca una ciudad", Toast.LENGTH_SHORT).show();
          return;
        }
        intentDetails.putExtra("JSON_DATA", cityData.toString());
        startActivity(intentDetails);
        
      }
    });
    return rootView;
  }
  
  public void getMinDetailsFromSearch(String pCity){
    try {
      cityData = WeatherHttpClient.
          getDataFromLocation(
              "q="+ pCity);
      if(cityData == null) {
        throw new JSONException("");
      }
      fillData();

    } catch(JSONException e) {
      Toast.makeText(getActivity(), "No hay internet", Toast.LENGTH_SHORT).show();
    }
    
  }

  private void fillData() throws JSONException {
    JSONArray prediction = cityData.getJSONArray("list");
    String ciudad = cityData.getJSONObject("city").getString("name"), 
      pais = cityData.getJSONObject("city").getString("country");
    int max = prediction.getJSONObject(0).getJSONObject("temp").getInt("max");
    int min = prediction.getJSONObject(0).getJSONObject("temp").getInt("min");
    
    txtSearchCity.setText(ciudad + ", " + pais);
    txtSearchCity.setTextSize(45);
    txtResultTempMin.setText(min + "");
    txtResultTempMin.setTextSize(30);
    txtResultTempMax.setText(max + "");
    txtResultTempMax.setTextSize(30);
  }

  @Override
  public void onPause() {
    super.onPause();
    if(cityData != null) {
      SharedPreferences sharedPref = (SharedPreferences)PreferenceManager.
            getDefaultSharedPreferences(getActivity());
      sharedPref.edit()
        .putString("JSON_DATA", cityData.toString())
        .commit();
    }
    
  }

  @Override
  public void onResume() {
    super.onResume();
    SharedPreferences sharedPref = (SharedPreferences)PreferenceManager.
        getDefaultSharedPreferences(getActivity());
    try {
      cityData = new JSONObject(sharedPref.getString("JSON_DATA", ""));
      fillData();
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
  }

}




Java Source Code List

org.das.coolweather.activities.DetailsActivityHost.java
org.das.coolweather.activities.SettingsActivity.java
org.das.coolweather.adapters.DayInfo.java
org.das.coolweather.adapters.DayListAdapter.java
org.das.coolweather.fragmentactivities.MainActivity.java
org.das.coolweather.fragments.Details.java
org.das.coolweather.fragments.Graph.java
org.das.coolweather.fragments.Map.java
org.das.coolweather.fragments.Search.java
org.das.coolweather.utils.LaBD.java
org.das.coolweather.utils.WeatherHttpClient.java