简体   繁体   English

来自alertdialog onclicklistener的调用方法(片段)

[英]Call method from alertdialog onclicklistener (in fragment)

I am stuck in seemingly simple thing but couldn't find a way. 我被看似简单的事情所困扰,但却找不到办法。 I have a method and an alertdialog inside a fragment. 我在片段中有一个方法和一个Alertdialog。 I want to call that method from alertdialog's onclickListener. 我想从alertdialog的onclickListener调用该方法。

The issue is, the method is not recognizable from in side on click listener. 问题是,无法从点击侦听器的侧面识别该方法。 Do I need to use separate DialogFragment and interface? 我是否需要使用单独的DialogFragment和接口? Is there any straight forward way without creating an extra class? 有没有创建额外的类的直接方法?

public class myFragment extends Fragment {
    public void samplefile(){
        AlertDialog.Builder myDialog = new AlertDialog.Builder(getActivity());
        myDialog.setTitle("Dialog Title");
        String[] filetypelist = {"A","B","C"};
        myDialog.setItems(filetypelist, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                ((SettingsFragment) getActivity()).dloadSample(which);
            }
        }); 
        myDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        myDialog.create().show();
    }

    public void dloadSample(int args) {
        //SOMETHING TO DO HERE
    }
}

Add this in your method: 在您的方法中添加:

getActivity()
    .getFragmentManager()
    .beginTransaction()
    .replace(android.R.id.content, new nameOfYourFragmentToBeCalled())
    .commit();`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM