代码之家  ›  专栏  ›  技术社区  ›  Shubham Khatri

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

  •  0
  • Shubham Khatri  · 技术社区  · 8 年前

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

    我有一个 ExpandableListView DialogFragment 班我想在单击中的TextView元素时启动dialogFragment类 childView 名单

    下面是 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;
            }
    
    .....
    
    }
    

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

    无法解析方法 getSupportFragmentManager()

    我知道 TestList 类不扩展 FragmentActivity 类,但我不能这样做,因为它已经扩展了 BaseExpandableListAdapter

    我也试着使用 getActivity().getSupportFragmentManager() 但在这里,我也得到了一个警告 Activity 班级不能有 getActivity() 方法,这对于 Fragment .

    下面是对话框类的代码

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

    如何解决上述问题。

    谢谢你的帮助

    1 回复  |  直到 8 年前
        1
  •  1
  •   Nishan Khadka    8 年前

    在对话框片段类中创建静态新实例方法

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

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

    RepeatSettings obj =  RepeatSettings.newInstance();
    
    obj.show(((MainActivity)context).getSupportFragmentManager(), DIALOG_SINGLE_CHOICE_LIST);
    
    推荐文章