Java tutorial
/* * Copyright 2013 Google Inc. * * 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 com.pentapus.pentapusdmh.Fragments.EncounterPrep.AddMonster.WizardMonsterEdit; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.ListFragment; import android.text.Editable; import android.text.TextWatcher; import android.util.SparseBooleanArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import com.pentapus.pentapusdmh.R; import com.wizardpager.wizard.model.Page; import com.wizardpager.wizard.ui.PageFragmentCallbacks; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class DmgVulFragment extends ListFragment { private static final String ARG_KEY = "key"; private PageFragmentCallbacks mCallbacks; private String mKey; private List<String> mChoices; private Page mPage; private ArrayList<String> selections; public static DmgVulFragment create(String key) { Bundle args = new Bundle(); args.putString(ARG_KEY, key); DmgVulFragment fragment = new DmgVulFragment(); fragment.setArguments(args); return fragment; } public DmgVulFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle args = getArguments(); mKey = args.getString(ARG_KEY); mPage = mCallbacks.onGetPage(mKey); selections = new ArrayList<String>(); DmgVulChoicePage fixedChoicePage = (DmgVulChoicePage) mPage; mChoices = new ArrayList<String>(); for (int i = 0; i < fixedChoicePage.getOptionCount(); i++) { mChoices.add(fixedChoicePage.getOptionAt(i)); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_page_dmgvul, container, false); ((TextView) rootView.findViewById(android.R.id.title)).setText(mPage.getTitle()); final ListView listView = (ListView) rootView.findViewById(android.R.id.list); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, mChoices)); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // Pre-select currently selected items. new Handler().post(new Runnable() { @Override public void run() { ArrayList<String> selectedItems = mPage.getData().getStringArrayList(mKey); if (selectedItems == null || selectedItems.size() == 0) { return; } Set<String> selectedSet = new HashSet<String>(selectedItems); for (int i = 0; i < mChoices.size(); i++) { if (selectedSet.contains(mChoices.get(i))) { selections.add(mChoices.get(i)); listView.setItemChecked(i, true); } } mPage.getData().putStringArrayList(Page.SIMPLE_DATA_KEY, selections); mPage.notifyDataChanged(); } }); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); if (!(activity instanceof PageFragmentCallbacks)) { //throw new ClassCastException("Activity must implement PageFragmentCallbacks"); mCallbacks = (PageFragmentCallbacks) getParentFragment(); } else { mCallbacks = (PageFragmentCallbacks) activity; } } @Override public void onDetach() { super.onDetach(); mCallbacks = null; } @Override public void onListItemClick(ListView l, View v, int position, long id) { SparseBooleanArray checkedPositions = getListView().getCheckedItemPositions(); if (selections != null) { selections = new ArrayList<String>(); } for (int i = 0; i < checkedPositions.size(); i++) { if (checkedPositions.valueAt(i)) { selections.add(getListAdapter().getItem(checkedPositions.keyAt(i)).toString()); } } mPage.getData().putStringArrayList(Page.SIMPLE_DATA_KEY, selections); mPage.notifyDataChanged(); } }