user3650467 user3650467 - 3 months ago 20
Android Question

DialogFragment in Tabbed Activity wont show

The Fragment is part of a Tabbed Activity. I was able to succesfully modify the Tabbed Activity template of Android Studio 2.1.2. Now I have changed it to the point where all the Fragments inside it have a FAB of their own. When Floating Action Button is clicked it is supposed to show a dialog. I have created a DialogFragment following the instruction from the official docs. The FAB uses setOnClickListener to refer to the method in the java file. I can't use

newEdQua.show(getSupportFragmentManager(), "createNewEdQua")


to show the dialog. it says it cannot resolve it

I also noticed that it won't load the ListView with the content of the Stinrg Array.

In summary I want to show a dialog from a Fragment(thats part of Tabbed Activity).

I have tried to clean and rebuild, sync gradle files with project. I dont want to mess with IDE configuration files. At least not now cause I'm far into a very important project(so suppressing warnings is not an option).

This is my code. It includes only areas with problem.

Dialog Fragment (to be shown):

public class createNewEdQua extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle saveSavedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

builder.setTitle(R.string.createnewEdQua);

builder.setMessage("New Qualification HERE!!");

builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});

return builder.create();
}
}


Fragment (placed as tab in Tabbed Activity):

public class edSET_Dashboard_Teachers extends Fragment {

FragmentManager fm;

//neInstance() method return reference to fragment
public static edSET_Dashboard_Teachers newInstance(){
edSET_Dashboard_Teachers fragment = new edSET_Dashboard_Teachers();
return fragment;
}


public edSET_Dashboard_Teachers() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

//following code is in public View onCreateView() method
String[] edqua={"Conten Here","Contend Here","Content Here"};

//had to use this cause findViewById() method was not working as it should
View view = inflater.inflate(R.layout.fragment_edset__dashboard__teachers, container, false);


ArrayAdapter<String> listOfEdqua2 = new ArrayAdapter<String>(getContext(), R.layout.list_item_edqua, edqua);//i tried using this as the context but error

ListView listView = (ListView)view.findViewById(R.id.edu_info);
listView.setAdapter(listOfEdqua2);


//had to use setOnClickListener cause android:onClick in xml does not work with Fragments ive learned
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab_edset);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment newEdQua = new createNewEdQua();
newEdQua.show(getActivity().getFragmentManager(), "createNewEdQua");

//addEdQua(view);
}
});


// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_edset__dashboard__teachers, container, false);

}

//the method to show the dialog
/*
public void addEdQua(View view){

}*/

}


When I run it on a real device while connected to my computer, and I tap the FAB nothing at all happens, no crash no error nothing.

Answer

I figured it out

return inflater.inflate(R.layout.fragment_edset__dashboard__teachers, container, false);

should be

return view;//it already inflates the Fragment layout

you are reinflating new view