我是 android 的初学者,坚持使用我认为这是一项简单的任务。
我有一个 ExpandableListView
和一个 DialogFragment
类。我想在单击列表的 childView
中的 TextView 元素时启动 dialogFragment 类
下面是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
类没有扩展 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/