I want to open a dialog from a subclass. However, there are some problems with the Show function. It does not take the subclass but the main class.
The Main Controller :
private void InitNavigation(){
MenuView = findViewById(R.id.bottomNavigationView);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment);
NavController navCo = navHostFragment.getNavController();
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.home2,
R.id.verantstaltung, R.id.veranstalter,R.id.tanzpartnersuche).build();
NavigationUI.setupActionBarWithNavController(this, navCo, appBarConfiguration);
NavigationUI.setupWithNavController(MenuView, navCo);
navCo.navigate(R.id.home2);
}
I think at the Funktion openDatePickerDialog is the error. With getActivity().getsupportFragmentManager i get the Main Class but here i will get the subclass The Sub Controller from which the dialog should be opened :
private void openDatePickerDialog(String type){
new DialogDatePicker(type).show(getActivity().getSupportFragmentManager(),"DialogDatePicker");
}
}
And Last the Dialog Controller :
package florian.tanzapp;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CalendarView;
import java.util.Calendar;
import java.util.Date;
import androidx.appcompat.app.AppCompatDialogFragment;
public class DialogDatePicker extends AppCompatDialogFragment {
private DialogDatePickerListener listener;
//DatePicker
CalendarView DatePicker;
//Var
String Type;
Date datum;
public DialogDatePicker(String type){
Type = type;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_datepicker,null);
builder.setView(view)
.setTitle("DatePicker")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
listener.applyDate(datum,Type);
}
});
DatePicker = view.findViewById(R.id.calendarView3);
DatePicker.setMinDate((new Date().getTime()));
DatePicker.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, month, dayOfMonth);
datum = newDate.getTime();
}
});
return builder.create();
}
@Override
public void onAttach(Context context){
super.onAttach(context);
try {
listener = (DialogDatePickerListener) context;
}
catch (ClassCastException e){
throw new ClassCastException(context.toString() + "must implement DialogDatePickerListener");
}
}
public interface DialogDatePickerListener{
void applyDate(Date datum, String type);
}
}
I don't have so much experience in Java because I am more specialized in C#. Does anyone see the problem what is wrong when I want to open the dialog from the subclasse ?