我是android的初学者并坚持使用,我认为这是一项简单的任务。
我有一个ExpandableListView
和一个DialogFragment
课程。当我单击List的childView
内的TextView元素时,我想启动dialogFragment类
以下是getChildView
ExpandableListView
方法
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
。
以下是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();
}
}
如何解决上述问题。
感谢您的帮助
答案 0 :(得分:1)
在Dialog片段类
中创建一个静态新实例方法 public static RepeatSettings newInstance() {
RepeatSettings frag = new RepeatSettings();
return frag;
}
然后在列表适配器中调用对话框片段,如下所示:由于您的MainActivity活动的上下文被传递给适配器。
RepeatSettings obj = RepeatSettings.newInstance();
obj.show(((MainActivity)context).getSupportFragmentManager(), DIALOG_SINGLE_CHOICE_LIST);