单击 ExpanandableListView 子元素的打开对话框片段

Open Dialog Fragment onClick of child element of an ExpanandableListView

我是 android 的初学者,我坚持认为这是一项简单的任务。

我有一个 ExpandableListView 和一个 DialogFragment class。当我单击 List

childView 内的 TextView 元素时,我想启动 dialogFragment class

下面是ExpandableListView

getChildView方法
public class TestList extends BaseExpandableListAdapter {

    private List<String> headers;
    private HashMap<String, HashMap<String, List<String> > > list_children;
    private Context context;

    private static final String DIALOG_SINGLE_CHOICE_LIST = "MainActivity.RepeatSettings";

.......

    @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            HashMap<String, List<String>> listHashMap = (HashMap<String, List<String>>) this.getChild(groupPosition,childPosition);
            if(convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.list_child, null);
            }

            TextView repeat_btn = (TextView) convertView.findViewById(R.id.repeat_btn);
            repeat_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RepeatSettings repeatSettings = new RepeatSettings();
                    repeatSettings.show(getSupportFragmentManager(), DIALOG_SINGLE_CHOICE_LIST );
                }
            });
            return convertView;
        }

.....

}

在上面的代码中我得到一个错误,

Cannot resolve method getSupportFragmentManager()

我知道我的 TestList class 没有扩展 FragmentActivity class,但我不能那样做,因为它已经扩展了 BaseExpandableListAdapter

我也尝试过使用 getActivity().getSupportFragmentManager() 但在这里我也收到一个警告,即 Activity class 不能有一个 getActivity() 方法,这很明显它将用于 Fragment.

下面是对话框的代码class

public class RepeatSettings extends DialogFragment {

    String repeat_interval[] ;
    String selected_interval;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // creating an alertDialog object
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        //defining the repeat_interval string
       repeat_interval = getResources().getStringArray(R.array.repeat_interval);

        //setting the alert dialog title and the type of items contained in it.
        //First parameter is the list, second is the already checked item, third is the listner object
        builder.setTitle("Choose Repeat Interval").setSingleChoiceItems(repeat_interval, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        selected_interval = (String) repeat_interval[which];
                        break;
                    case 1:
                        selected_interval = (String) repeat_interval[which];
                        break;
                    case 2:
                        selected_interval = (String) repeat_interval[which];
                }
            }
        }).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        return builder.create();
    }
}

如何解决以上问题。

感谢您的帮助

在对话框片段中创建一个静态的新实例方法class

 public static RepeatSettings newInstance() {
RepeatSettings frag = new RepeatSettings();
 return frag;
}

然后在您的列表适配器中调用对话片段,如下所示:因为您的 MainActivity activity 的上下文已传递给适配器。

RepeatSettings obj =  RepeatSettings.newInstance();

obj.show(((MainActivity)context).getSupportFragmentManager(), DIALOG_SINGLE_CHOICE_LIST);