Java tutorial
/** * This file is part of ChessPositionManagerAndroid. ChessPositionManagerAndroid is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. ChessPositionManagerAndroid is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ChessPositionManagerAndroid. If not, see <http://www.gnu.org/licenses/>. */ package com.loloof64.android.chess_position_manager.file_explorer; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; import com.loloof64.android.chess_position_manager.R; import java.util.Arrays; /** * Created by laurent-bernabe on 24/12/14. */ public class ConfirmRemoveElementsDialogFragment extends DialogFragment { public final static String FOLDERS_TAG = "com.loloof64.android.chess_position_manager.file_explorer.folders"; public final static String POSITIONS_TAG = "com.loloof64.android.chess_position_manager.file_explorer.positions"; public interface ConfirmRemoveElementsListener { void onConfirmRemoveElementsDialogPositiveClick(String[] foldersNames, String[] filesNames); } @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); final View view = inflater.inflate(R.layout.remove_elements_confirm_dialog_layout, null); TextView foldersView = (TextView) view.findViewById(R.id.removing_elements_folders_list); TextView positionsView = (TextView) view.findViewById(R.id.removing_elements_positions_list); String[] tempFolders = new String[0]; String[] tempPositions = new String[0]; Bundle arguments = getArguments(); if (arguments != null) { tempFolders = arguments.getStringArray(FOLDERS_TAG); tempPositions = arguments.getStringArray(POSITIONS_TAG); } Arrays.sort(tempFolders); Arrays.sort(tempPositions); final String[] folders = tempFolders; final String[] positions = tempPositions; StringBuilder foldersTxtBuilder = new StringBuilder(); StringBuilder positionsTxtBuilder = new StringBuilder(); for (String currentFolder : folders) { foldersTxtBuilder.append(String.format("-) %s\n", currentFolder)); } for (String currentPosition : positions) { positionsTxtBuilder.append(String.format("-) %s\n", currentPosition)); } foldersView.setText(folders.length > 0 ? foldersTxtBuilder : getString(R.string.none)); positionsView.setText(positions.length > 0 ? positionsTxtBuilder : getString(R.string.none)); builder.setTitle(R.string.title_removing_elements).setView(view) .setPositiveButton(R.string.action_validate, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { listener.onConfirmRemoveElementsDialogPositiveClick(folders, positions); } }).setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ConfirmRemoveElementsDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the dialogListener so we can send events to the host listener = (ConfirmRemoveElementsListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement ConfirmRemoveElementsListener"); } } private ConfirmRemoveElementsListener listener; }