片段对话框连接到一个片段不叫onYes /否方法片段、不叫、连接到、对话框

2023-09-03 22:45:18 作者:微笑背后的殇

第一个:我不需要对如何创建并显示一个对话框教训,看了都觉得我写的,请

我有一个问题,这是我的第一个问题在这里。我有一个主要活动的应用程序与抽屉左,当我点击一个项目它显示了一个片段的主要内容。一个片段让我在SQLite数据库添加一个星球。然后另一片段显示我与我在数据库中插入行星列表。点击其中一个吧, FragmentDialog 将出现,并问我,如果我想删除这个星球:如果是删除它,如果不是什么都不做。该方法的 onYes ()似乎为不会被调用,我想。有人知道为什么吗? 这是在类的 FragmentDialog

 公共类FragmentDialog扩展DialogFragment
{
    私人IFragmentDialog mListener;

    公共静态FragmentDialog的getInstance()
    {
        返回新FragmentDialog();
}

   公共接口IFragmentDialog
    {
        公共无效onYes();
        公共无效ONNO();
    }

    @覆盖
    公共对话onCreateDialog(包savedInstanceState)
    {
        AlertDialog.Builder vDialogBu​​ilder =新AlertDialog.Builder(getActivity());
        vDialogBu​​ilder.setTitle(Attenzione);
        vDialogBu​​ilder.setMessage(Vuoi cancellare questo ELEMENTO?);
        vDialogBu​​ilder.setPositiveButton(SI,新DialogInterface.OnClickListener()
        {
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释其)
            {
                如果(mListener!= NULL)
                {
                    mListener.onYes();
                }
            }
        });

        vDialogBu​​ilder.setNegativeButton(否,新DialogInterface.OnClickListener()
        {
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释其)
            {
                如果(mListener!= NULL)
                {
                    mListener.onNo();
                }
            }
        });

        返回vDialogBu​​ilder.create();
    }

    @覆盖
    公共无效onAttach(活动活动)
    {
        super.onAttach(活动);
        如果(活动的instanceof IFragmentDialog)
        {
            mListener =(IFragmentDialog)的活动;
        }
    }
}
 

这是在类的片段将打开FragmentDialog:

 公共类FragmentPlanetList扩展片段实现LoaderManager.LoaderCallbacks<光标>中IFragmentDialog
{
    ListView控件mListView;
    PlanetCursorAdapter mCursorAdapter;
    私人ILoadFragment mListener;

    公共静态开放的URI = NULL;

    私有静态最终诠释PLANET_LOADER_ID = 0;

    公共静态FragmentPlanetList的getInstance()
{
    返回新FragmentPlanetList();
}

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
    查看vView = inflater.inflate(R.layout.fragment_planet_list,集装箱,假);
    。getActivity()的setTitle(R.string.fragment_planet_list);

    mListView =(ListView控件)vView.findViewById(R.id.planet_list);

    mCursorAdapter =新PlanetCursorAdapter(getActivity()getApplicationContext(),NULL,0);

    mListView.setAdapter(mCursorAdapter);

    。getLoaderManager()initLoader(PLANET_LOADER_ID,空,这一点);

    mListView.setOnItemClickListener(新OnItemClickListener()
    {
        @覆盖
            公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长ID)
            {
            的uri = Uri.parse(PlanetProvider.PLANETS_URI +/+ ID);
 

// getActivity()getContentResolver()删除(URI,NULL,NULL);                 Toast.makeText(getActivity(),Cancellato ID+的id,Toast.LENGTH_SHORT).show();

 的ShowDialog();
        }
    });

    返回vView;
}

保护无效的ShowDialog()
{
    FragmentDialog vDialog = FragmentDialog.getInstance();
    vDialog.show(getFragmentManager(),SHOW_DIALOG);
}

@覆盖
公共无效onAttach(活动活动)
{
    super.onAttach(活动);
    如果(活动的instanceof ILoadFragment)
    {
        mListener =(ILoadFragment)的活动;
    }
}

    @覆盖
    公共无效onYes()
    {
        //不叫
        Toast.makeText(getActivity(),A,Toast.LENGTH_LONG).show();
 
梯形ABCD中,AD BC,点E是AB中点,连接EC,ED CE DE,CD,AD,BC,三条线段之间有什么样的数量关系 请说明理由

// getActivity()getContentResolver()删除(URI,NULL,NULL);         }

  @覆盖
    公共无效ONNO()
    {

    }
}
 

修改:我觉得这个问题是(如 OrhanC1 称)的若 onAttach ()方法。有我,也能实现我的活动FragmentDialog接口?如果是这样,我怎么可以通过列表项的位置和URI删除?

解决方案

一个DialogFragment真的只是包装了一个对话的片段。你可以把任何类型的对话框在那里创建和DialogFragment的onCreateDialog()方法返回的对话框。

在onCreateDialog任意数量的按钮创建自定义对话框(); 在给定的code OK是禁止通过复制code作为setNegativeButton做。你可以启用它

更换新DialogInterface等等等等空。如果你喜欢

下面有一个例子DialogFragment:

 类MyDialogFragment扩展DialogFragment {
    语境mContext;
    公共MyDialogFragment(){
        mContext = getActivity();
    }
    @覆盖
    公共对话onCreateDialog(包savedInstanceState){
        AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(mContext);
        alertDialogBu​​ilder.setTitle(真的吗?);
        alertDialogBu​​ilder.setMessage(你确定吗?);
        //空应该是你的点击监听器
        alertDialogBu​​ilder.setPositiveButton(OK,NULL);
        alertDialogBu​​ilder.setNegativeButton(取消,新DialogInterface.OnClickListener(){

            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                dialog.dismiss();
            }
        });


        返回alertDialogBu​​ilder.create();
    }
}
 

要创建对话框电话:

 新MyDialogFragment()秀(getFragmentManager(),MyDialog)。
 

和从什么地方关闭对话框:

((MyDialogFragment)getFragmentManager().findFragmentByTag("MyDialog")).getDialog().dismiss();

所有这些code会很好地工作的支持库,通​​过只是改变了进口使用支持库类。

FIRST: I don't need lessons on how creating and showing a Dialog, read all that I wrote please.

I have a problem and this is my first question here. I have an application with a Main Activity with a drawer on the left that when I click on an item it shows a Fragment in the main content. One Fragment let me add a planet in a SQLite Database. Then another Fragment shows me a list with the planets that I inserted in the database. Clicking on one of it, a FragmentDialog will appear and asks me if I want to delete the planet: if yes delete it, if not does nothing. The method onYes() seems to not being called, I think. Someone know why? This is the Class for the FragmentDialog:

public class FragmentDialog extends DialogFragment 
{   
    private IFragmentDialog mListener;

    public static FragmentDialog getInstance()
    {
        return new FragmentDialog();
}

   public interface IFragmentDialog
    {
        public void onYes();
        public void onNo();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {       
        AlertDialog.Builder vDialogBuilder = new AlertDialog.Builder(getActivity());
        vDialogBuilder.setTitle("Attenzione");
        vDialogBuilder.setMessage("Vuoi cancellare questo elemento?");
        vDialogBuilder.setPositiveButton("Sì", new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                if (mListener != null)
                {
                    mListener.onYes();
                }
            }
        });

        vDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                if (mListener != null)
                {
                    mListener.onNo();
                }
            }
        });

        return vDialogBuilder.create();
    }

    @Override
    public void onAttach(Activity activity) 
    {
        super.onAttach(activity);
        if (activity instanceof IFragmentDialog)
        {
            mListener = (IFragmentDialog) activity;
        }
    }
}

And this is the Class for the Fragment that will open the FragmentDialog:

public class FragmentPlanetList extends Fragment implements     LoaderManager.LoaderCallbacks<Cursor>, IFragmentDialog
{
    ListView mListView;
    PlanetCursorAdapter mCursorAdapter;
    private ILoadFragment mListener;

    public static Uri uri = null;

    private static final int PLANET_LOADER_ID = 0;

    public static FragmentPlanetList getInstance()
{
    return new FragmentPlanetList();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View vView = inflater.inflate(R.layout.fragment_planet_list, container, false);
    getActivity().setTitle(R.string.fragment_planet_list);

    mListView = (ListView) vView.findViewById(R.id.planet_list);

    mCursorAdapter = new PlanetCursorAdapter(getActivity().getApplicationContext(), null, 0);

    mListView.setAdapter(mCursorAdapter);

    getLoaderManager().initLoader(PLANET_LOADER_ID, null, this);

    mListView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {
            uri = Uri.parse(PlanetProvider.PLANETS_URI + "/" + id);

// getActivity().getContentResolver().delete(uri, null, null); Toast.makeText(getActivity(), "Cancellato ID " + id, Toast.LENGTH_SHORT).show();

            showDialog();
        }
    });

    return vView;
}

protected void showDialog() 
{
    FragmentDialog vDialog = FragmentDialog.getInstance();
    vDialog.show(getFragmentManager(), "SHOW_DIALOG");
}

@Override
public void onAttach(Activity activity) 
{
    super.onAttach(activity);
    if (activity instanceof ILoadFragment)
    {
        mListener = (ILoadFragment) activity;
    }
}

    @Override
    public void onYes() 
    {
        // not called
        Toast.makeText(getActivity(), "A", Toast.LENGTH_LONG).show();

// getActivity().getContentResolver().delete(uri, null, null); }

    @Override
    public void onNo() 
    {

    }
}

EDIT: I think that the problem is (like OrhanC1 said) the if in onAttach() method. Have I to implement the FragmentDialog interface also in my Activity? If so, how can I pass the position of the list item and the uri to delete?

解决方案

A DialogFragment is really just a fragment that wraps a dialog. You can put any kind of dialog in there by creating and returning the dialog in the onCreateDialog() method of the DialogFragment.

Create your Custom Dialog with any number of button in onCreateDialog(); In the given code ok is disable you can enable it by copying the code as the setNegativeButton Do.

Replace null with new DialogInterface blah blah . if you like

Heres an example DialogFragment:

class MyDialogFragment extends DialogFragment{
    Context mContext;
    public MyDialogFragment() {
        mContext = getActivity();
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
        alertDialogBuilder.setTitle("Really?");
        alertDialogBuilder.setMessage("Are you sure?");
        //null should be your on click listener
        alertDialogBuilder.setPositiveButton("OK", null);
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });


        return alertDialogBuilder.create();
    }
}

To create the dialog call:

new MyDialogFragment().show(getFragmentManager(), "MyDialog");

And to dismiss the dialog from somewhere:

((MyDialogFragment)getFragmentManager().findFragmentByTag("MyDialog")).getDialog().dismiss();

All of that code will work perfectly with the support library, by just changing the imports to use the support library classes.