android - 单击 ExpanandableListView 子元素的打开对话框 fragment

标签 android android-fragments expandablelistview

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

我有一个 ExpandableListView 和一个 DialogFragment 类。我想在单击列表的 childView 中的 TextView 元素时启动 dialogFragment 类

下面是ExpandableListViewgetChildView方法

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 类没有扩展 FragmentActivity 类,但我不能这样做,因为它已经扩展了 BaseExpandableListAdapter

我也尝试过使用 getActivity().getSupportFragmentManager() 但在这里我也收到一个警告,提示 Activity 类不能有 getActivity () 方法,这对于 Fragment 来说是显而易见的。

下面是Dialog类的代码

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();
    }
}

如何解决上述问题。

感谢您的帮助

最佳答案

在Dialog fragment 类中创建一个Static new Instance方法

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

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

RepeatSettings obj =  RepeatSettings.newInstance();

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

关于android - 单击 ExpanandableListView 子元素的打开对话框 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41294735/

相关文章:

android - 如何在 android 平台上的可执行文件中使用 CallStack(在 CallStack.tpp 中)?

java - NavigationComponents - 抽屉导航无法导航

android-fragments - 使用 Navigation - Kotlin 从工具栏菜单项单击的片段之间传递数据

android - 隐藏项目点击的所有项目,但可扩展 ListView 的点击项目除外

android - 如何在 ExpandableListView 中组合子项及其标题?

android - 使用 SimpleCursorTreeAdapter 保存和恢复 ExpandableListActivity 的折叠状态

android - 在谷歌地图上方显示工具栏

android - append 视频无法正常工作 - Android

android - 如何查看flutter app的混淆结果?

android - AppBarLayout 在滚动行为上隐藏工具栏,切换 fragment