Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / MapsDialogFragment.java
package com.example.nemophila;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.example.nemophila.entities.Shop;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class MapsDialogFragment extends DialogFragment {

    private Collection<Shop> shops;
    public MapsDialogFragment(Collection<Shop> shops) {
        this.shops = shops;
    }
    Nemophila nemophila;


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        nemophila = (Nemophila) getActivity().getApplication();

        String[] choices = new String[shops.size()];
        //ArrayList<String> choices = new ArrayList<>();
        Shop[] shoplist = new Shop[shops.size()];

        int j = 0;
        for (Iterator i = shops.iterator(); i.hasNext(); j++) {
            Shop tmp = (Shop)i.next();
            choices[j] = tmp.getName();

            shoplist[j] = tmp;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("投稿したいお店はこの中にありますか?")
                .setPositiveButton("該当する店がありません(新規作成)", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //このボタンを押した時の処理を書きます。
                        //新規投稿画面へ遷移
                        //ShopCreate画面に遷移
                        Intent intent = new Intent(getActivity(), ShopCreateActivity.class);
                        startActivity(intent);
                    }
                })
                .setNeutralButton("キャンセル", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // このボタンを押した時の処理を書きます。
                    }
                })
                .setItems(choices, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(),
                                        String.format("「%s」を選択しました。", choices[which]),
                                        Toast.LENGTH_SHORT)
                                .show();

                        //そのShopをNemophilaに渡す
                        nemophila.setCurrentShop(shoplist[which]);

                        //そのShopのPostActivityに遷移
                        //PostCreate画面に遷移
                        Intent intent = new Intent(getActivity(), PostActivity.class);
                        startActivity(intent);
                    }
                });
        return builder.create();
    }
}