Java tutorial
/** Copyright (c) 2014 Jordi Lpez <@wuyingren> * * MIT License: * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ package cat.wuyingren.rorhelper.fragments.dialogs; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.CompoundButton; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.Random; import cat.wuyingren.rorhelper.R; import cat.wuyingren.rorhelper.activities.MainActivity; import cat.wuyingren.rorhelper.profiles.ManageSenatorResult; import cat.wuyingren.rorhelper.profiles.Senator; import cat.wuyingren.rorhelper.sql.GameDataSource; import cat.wuyingren.rorhelper.utils.SenatorAdapter; import cat.wuyingren.rorhelper.utils.SystemUtils; /** * Created by jordilb on 28/10/14. */ public class ManageSenatorDialogFragment extends DialogFragment { public static final String ARG_MODE = "mode"; public static final String ARG_TYPE = "type"; public static final String ARG_GAMEID = "gameid"; public static final String ARG_ARRAY = "array"; public static final String ARG_VALUES = "values"; private ManageSenatorResult result = new ManageSenatorResult(); private GameDataSource dataSource; private long currentGameId = 0; private Context context; private boolean killMode = false; private boolean statesman = false; private boolean randomkill = false; /* The activity that creates an instance of this dialog fragment must * implement this interface in order to receive event callbacks. * Each method passes the DialogFragment in case the host needs to query it. */ public interface ManageSenatorDialogListener { public void onManageSenatorDialogPositiveClick(ManageSenatorResult result); } // Use this instance of the interface to deliver action events ManageSenatorDialogListener mListener; @Override public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (ManageSenatorDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement ManageSenatorDialogListener"); } } @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); final View v = inflater.inflate(R.layout.dialog_managesenator, null); final TextView randomKillTitle = (TextView) v.findViewById(R.id.managesenator_randomTitle); final ToggleButton randomKillButton = (ToggleButton) v.findViewById(R.id.managesenator_randomToggle); final LinearLayout selectors = (LinearLayout) v.findViewById(R.id.managesenator_selectors); dataSource = MainActivity.getDataSource(); context = MainActivity.getContext(); currentGameId = getArguments().getLong(ARG_GAMEID); killMode = getArguments().getBoolean(ARG_MODE); statesman = getArguments().getBoolean(ARG_TYPE); result.setKill(killMode); result.setStatesman(statesman); final Spinner senatorSpinner = (Spinner) v.findViewById(R.id.managesenator_senatorSpinner); randomKillButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { result.setRandomKill(isChecked); if (isChecked) { selectors.setVisibility(View.GONE); } else { selectors.setVisibility(View.VISIBLE); } } }); if (!killMode) { randomKillTitle.setVisibility(View.GONE); randomKillButton.setVisibility(View.GONE); String[] keys = getArguments().getStringArray(ARG_ARRAY); ArrayList<String> values = getArguments().getStringArrayList(ARG_VALUES); /*ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(MainActivity.getContext(), android.R.layout.simple_spinner_item, keys);*/ SenatorAdapter adapter = new SenatorAdapter(MainActivity.getContext(), android.R.layout.simple_spinner_item, keys, values); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); senatorSpinner.setAdapter(adapter); } else { randomKillTitle.setVisibility(View.VISIBLE); randomKillButton.setVisibility(View.VISIBLE); } senatorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { result.setSenatorID(parent.getSelectedItem().toString()); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); final Spinner factionSpinner = SystemUtils.configureSpinner(v, R.id.managesenator_factionSpinner, R.array.faction_nameID); factionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { long factionID = dataSource.getFactionId(currentGameId, getResources().getStringArray(R.array.faction_nameID)[position]); result.setFactionID(factionID); if (killMode) { ArrayList<Senator> senators = dataSource.getAllSenatorsOfFaction(factionID); Iterator<Senator> ite = senators.iterator(); ArrayList<CharSequence> array = new ArrayList<CharSequence>(); ArrayList<String> values = new ArrayList<String>(); while (ite.hasNext()) { Senator s = ite.next(); array.add(String.valueOf(s.getId())); values.add(String.valueOf(s.getName())); } String[] keys = new String[array.size()]; keys = array.toArray(keys); //ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context, android.R.layout.simple_spinner_item, array); SenatorAdapter adapter = new SenatorAdapter(context, android.R.layout.simple_spinner_item, keys, values); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); senatorSpinner.setAdapter(adapter); } } @Override public void onNothingSelected(AdapterView<?> parent) { } }); builder.setView(v).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mListener.onManageSenatorDialogPositiveClick(result); } }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { getDialog().cancel(); } }); String stringA, stringB; if (killMode) { stringA = getString(R.string.dialog_managesenator_kill_title); } else { stringA = getString(R.string.dialog_managesenator_title); } if (statesman) { stringB = getString(R.string.statesman); } else { stringB = getString(R.string.senator); } builder.setTitle(stringA + " " + stringB); return builder.create(); } }