Android Open Source - UK-Weather-repo City Search Results Dialog






From Project

Back to project page UK-Weather-repo.

License

The source code is released under:

Apache License

If you think the Android project UK-Weather-repo 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 com.haringeymobile.ukweather;
//from   www  .  j  av a 2 s  .  c  om
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * A dialog displaying the list of found cities in response for the user's
 * search query.
 */
public class CitySearchResultsDialog extends DialogFragment {

  /** A listener for the found city list item clicks. */
  public interface OnCityNamesListItemClickedListener {

    /**
     * Reacts to the city list item clicks.
     * 
     * @param position
     *            clicked item position in the city list
     */
    void onFoundCityNamesItemClicked(int position);

  }

  static final String CITY_NAME_LIST = "city names";

  private OnCityNamesListItemClickedListener onCityNamesListItemClickedListener;
  private ListView listView;
  private CityNameArrayAdapter arrayAdapter;

  /**
   * Creates a new dialog with the city list.
   * 
   * @param cityNames
   *            an array of city names (including location coordinates) to be
   *            displayed as a list
   * @return a dialog displaying the list of specified city names
   */
  static CitySearchResultsDialog newInstance(ArrayList<String> cityNames) {
    CitySearchResultsDialog dialog = new CitySearchResultsDialog();
    Bundle args = new Bundle();
    args.putStringArrayList(CITY_NAME_LIST, cityNames);
    dialog.setArguments(args);
    return dialog;
  }

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
      onCityNamesListItemClickedListener = (OnCityNamesListItemClickedListener) activity;
    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString()
          + " must implement OnCityNamesListItemClickedListener");
    }
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_search_results,
        container);

    createCustomDialogTitle(view);

    listView = (ListView) view.findViewById(android.R.id.list);
    setCityListView();

    return view;
  }

  /**
   * Replaces the default dialog's title with the custom one.
   * 
   * @param view
   *            custom dialog fragment's view
   */
  private void createCustomDialogTitle(View view) {
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    TextView customDialogTitle = (TextView) view
        .findViewById(R.id.city_search_dialog_title);
    customDialogTitle.setText(R.string.dialog_title_search_results);
  }

  /**
   * Prepares the city list for display and item clicks.
   */
  private void setCityListView() {
    listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        onCityNamesListItemClickedListener
            .onFoundCityNamesItemClicked(position);
        dismiss();
      }

    });
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (arrayAdapter == null) {
      initialiseArrayAdapter();
    }
    listView.setAdapter(arrayAdapter);
  }

  /**
   * Creates a new adapter to map city names to the list rows.
   */
  private void initialiseArrayAdapter() {
    Bundle args = getArguments();
    ArrayList<String> cityNames = args.getStringArrayList(CITY_NAME_LIST);
    arrayAdapter = new CityNameArrayAdapter(getActivity(),
        R.layout.row_city_search_list, cityNames);
  }

  @Override
  public void onDestroyView() {
    listView.setAdapter(null);
    super.onDestroyView();
  }

  @Override
  public void onDetach() {
    super.onDetach();
    onCityNamesListItemClickedListener = null;
  }

  /** A helper to implement the "view holder" design pattern. */
  private static class CityNameViewHolder {
    TextView cityNameTextView;
  }

  /** An adapter to map city names to the list rows. */
  private class CityNameArrayAdapter extends ArrayAdapter<String> {

    private Activity context;
    private int layoutResourceId;
    private final List<String> cityNames;

    private CityNameArrayAdapter(Activity activity, int layoutResourceId,
        List<String> cityNames) {
      super(activity, layoutResourceId, cityNames);
      this.context = activity;
      this.layoutResourceId = layoutResourceId;
      this.cityNames = cityNames;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      CityNameViewHolder holder;
      View rowView = convertView;
      if (rowView == null) {
        rowView = context.getLayoutInflater().inflate(layoutResourceId,
            parent, false);
        holder = new CityNameViewHolder();
        holder.cityNameTextView = (TextView) rowView
            .findViewById(R.id.city_name_in_list_row_text_view);
        rowView.setTag(holder);
      }
      holder = (CityNameViewHolder) rowView.getTag();

      String cityName = cityNames.get(position);
      holder.cityNameTextView.setText(cityName);

      setBackgroundForListRow(position, rowView);

      return rowView;
    }

    /**
     * Makes the list to look nicer by setting alternating bacgrounds to
     * it's items (rows).
     * 
     * @param position
     *            city list position
     * @param rowView
     *            a view displaying a single list item
     */
    private void setBackgroundForListRow(int position, View rowView) {
      if (position % 2 == 1) {
        rowView.setBackgroundResource(BaseCityCursorAdapter.BACKGROUND_RESOURCE_ODD);
      } else {
        rowView.setBackgroundResource(BaseCityCursorAdapter.BACKGROUND_RESOURCE_EVEN);
      }
    }

  }

}




Java Source Code List

com.astuetz.PagerSlidingTabStrip.java
com.haringeymobile.ukweather.AboutActivity.java
com.haringeymobile.ukweather.BaseCityCursorAdapter.java
com.haringeymobile.ukweather.BaseCityListFragmentWithButtons.java
com.haringeymobile.ukweather.CityListFragmentWithUtilityButtons.java
com.haringeymobile.ukweather.CityListFragmentWithWeatherButtons.java
com.haringeymobile.ukweather.CityManagementActivity.java
com.haringeymobile.ukweather.CitySearchResultsDialog.java
com.haringeymobile.ukweather.CityUtilitiesCursorAdapter.java
com.haringeymobile.ukweather.CityWeatherCursorAdapter.java
com.haringeymobile.ukweather.DeleteCityDialog.java
com.haringeymobile.ukweather.GetAvailableCitiesTask.java
com.haringeymobile.ukweather.MainActivity.java
com.haringeymobile.ukweather.SettingsActivityPreHoneycomb.java
com.haringeymobile.ukweather.SettingsActivity.java
com.haringeymobile.ukweather.SettingsFragment.java
com.haringeymobile.ukweather.WeatherCurrentInfoFragment.java
com.haringeymobile.ukweather.WeatherDailyWeatherForecastChildFragment.java
com.haringeymobile.ukweather.WeatherForecastParentFragment.java
com.haringeymobile.ukweather.WeatherInfoActivity.java
com.haringeymobile.ukweather.WeatherInfoFragment.java
com.haringeymobile.ukweather.WeatherInfoType.java
com.haringeymobile.ukweather.WeatherThreeHourlyForecastChildFragment.java
com.haringeymobile.ukweather.WorkerFragmentToRetrieveJsonString.java
com.haringeymobile.ukweather.data.InitialCity.java
com.haringeymobile.ukweather.data.JsonFetcher.java
com.haringeymobile.ukweather.data.OpenWeatherMapUrl.java
com.haringeymobile.ukweather.data.objects.CityCurrentWeather.java
com.haringeymobile.ukweather.data.objects.CityDailyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.CityInfo.java
com.haringeymobile.ukweather.data.objects.CityThreeHourlyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.Clouds.java
com.haringeymobile.ukweather.data.objects.Coordinates.java
com.haringeymobile.ukweather.data.objects.NumericParameters.java
com.haringeymobile.ukweather.data.objects.Rain.java
com.haringeymobile.ukweather.data.objects.SearchResponseForDailyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForFindQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForThreeHourlyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SystemParameters.java
com.haringeymobile.ukweather.data.objects.TemperatureScale.java
com.haringeymobile.ukweather.data.objects.Temperature.java
com.haringeymobile.ukweather.data.objects.WeatherInformation.java
com.haringeymobile.ukweather.data.objects.Weather.java
com.haringeymobile.ukweather.data.objects.WindSpeedMeasurementUnit.java
com.haringeymobile.ukweather.data.objects.Wind.java
com.haringeymobile.ukweather.database.CityTable.java
com.haringeymobile.ukweather.database.DatabaseHelper.java
com.haringeymobile.ukweather.database.GeneralDatabaseService.java
com.haringeymobile.ukweather.database.SqlOperation.java
com.haringeymobile.ukweather.database.WeatherContentProvider.java
com.haringeymobile.ukweather.utils.AsyncTaskWithProgressBar.java
com.haringeymobile.ukweather.utils.GlobalConstants.java
com.haringeymobile.ukweather.utils.MiscMethods.java
com.haringeymobile.ukweather.utils.SharedPrefsHelper.java