Java tutorial
/* * Copyright (c) 2014 Yehezkel (Zack) Yovel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package zack.yovel.clear.controller.foreground; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentManager; import android.util.Log; import android.widget.ArrayAdapter; import java.util.List; import zack.yovel.clear.R; import zack.yovel.clear.infrastructure.model.geonames.GeoName; public class SuggestionsDialogFragment extends DialogFragment implements DialogInterface.OnClickListener { public static final String EXTRA_SUGGESTIONS_LIST = "yovel.zack.clear.EXTRA_SUGGESTIONS_LIST"; public static final String EXTRA_TITLE = "yovel.zack.clear.EXTRA_TITLE"; private static final String TAG = "SuggestionsDialogFragment"; private List<GeoName> suggestions; private String title; private OnSelectionMade listener; private boolean showing; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { listener = (OnSelectionMade) activity; } catch (ClassCastException e) { Log.e(TAG, activity.getClass().getName() + " must implement " + OnSelectionMade.class.getName()); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle arguments = getArguments(); suggestions = arguments.getParcelableArrayList(EXTRA_SUGGESTIONS_LIST); title = arguments.getString(EXTRA_TITLE); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog output; AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(title); // If there are suggestions, show them if (suggestions.size() > 0) { ArrayAdapter<GeoName> adapter = new ArrayAdapter<GeoName>(getActivity(), android.R.layout.simple_list_item_1, suggestions); builder.setAdapter(adapter, this); } else { builder.setMessage(R.string.message_no_suggestions); } output = builder.create(); return output; } @Override public void show(FragmentManager manager, String tag) { if (manager.findFragmentByTag(tag) == null) { super.show(manager, tag); } } @Override public void onClick(DialogInterface dialog, int which) { GeoName selected = suggestions.get(which); listener.onSelectionMade(selected.name, selected.lat + "," + selected.lng); } public interface OnSelectionMade { void onSelectionMade(String locationName, String latlng); } }