OnCreateView not called in a dialogfragment from a fragment

浪尽此生 提交于 2019-12-11 03:37:10

问题


I have a fragment that executes or display a dialog fragment once a button is clicked. This dialog fragment displays a table generated by code, however when I try to click the button on this is displayed

And when I trace it in the LogCat the dialog fragments onCreateView is not called. Can somebody help or explain this to me?. Im not that good in android programming yet and I know i still have a lot to learn.

Here is the code of the fragment that calls the dialog fragment

public class fragment_schedule extends Fragment {

...............


public fragment_schedule(ArrayList<SubjSchedule> subj){
    subject = subj;
}
@SuppressWarnings("deprecation")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

..................

showlstbtn = (Button) rootView.findViewById(R.id.button_showlstschd);
        showlstbtn.setOnClickListener(new OnClickListener(){

            @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
            ft.addToBackStack(null);
            DialogFragment dialog = ShowLstDialog.newInstance(subject);
            dialog.show(ft, "dialog");
        }

    });

   ..........

and heres my dialog fragment

public class ShowLstDialog extends DialogFragment {

private static final String TAG = ShowLstDialog.class.getSimpleName();
public static Context mContext;
public static TableLayout tl;
public static TableRow trh;
private static ArrayList<SubjSchedule> subject= new ArrayList<SubjSchedule>(); 

public static DialogFragment newInstance(ArrayList<SubjSchedule> subj) {
    // TODO Auto-generated method stub
    DialogFragment f = new DialogFragment();
    subject = subj;
    Log.v(TAG, subject.size()+"");
    return f;
}



 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.v(TAG, "OnCreateView: " + subject.size()+"");
    View rootView = inflater.inflate(R.layout.fragment_dialog_showlst, container);
    //mEditText = (EditText) view.findViewById(R.id.txt_your_name);
    Log.v(TAG, "OnCreateView: " + subject.size()+"");
    getDialog().setTitle("");
    tl = (TableLayout) rootView.findViewById(R.id.tablelayout_schedlst);
    trh = new TableRow(getActivity());
    TextView[] tvh = new TextView[3];
    for(int i=0; i<3; i++){
        tvh[i] = new TextView(getActivity());
        tvh[i].setBackgroundResource(R.drawable.green2);
        tvh[i].setTextColor(getResources().getColor(R.color.LightCyan));
        tvh[i].setGravity(Gravity.CENTER);
        tvh[i].setPadding(30, 3, 30, 3);
        //tvh[i].setLayoutParams(params);
        trh.addView(tvh[i]);

    }

    tvh[0].setText("Subject");
    tvh[1].setText("Description");
    tvh[2].setText("Instructor");

    TableRow[] tr = new TableRow[subject.size()];
    for(int i=0; i<subject.size();i++){
        tr[i] = new TableRow(getActivity());
        TextView[] tv1 = new TextView[3];


        for(int k=0; k<3; k++){
            tv1[k] = new TextView(getActivity());
            tv1[k].setBackgroundResource(R.drawable.btn_default_disabled_holo_dark);
            tv1[k].setTextColor(getResources().getColor(R.color.list_background_pressed));
            tv1[k].setGravity(Gravity.CENTER);
            tv1[k].setPadding(30, 3, 30, 3);
            //tvh[i].setLayoutParams(params);
            tr[i].addView(tv1[i]);

        }

        tv1[0].setText(subject.get(i).getcn());
        tv1[1].setText(subject.get(i).getd());
        tv1[2].setText(subject.get(i).geti());
        tl.addView(tr[i]);

    }
    return rootView;
}



}

and here is the xml file of the dialog fragment

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@color/LightCyan"
        android:scrollbars="vertical" >

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal" >

                <TableLayout
                    android:id="@+id/tablelayout_schedlst"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:stretchColumns="*" >
                </TableLayout>
            </RelativeLayout>
        </HorizontalScrollView>
    </ScrollView>

</RelativeLayout>

thank you so much in advance.


回答1:


Change this

public static DialogFragment newInstance(ArrayList<SubjSchedule> subj) {
    // TODO Auto-generated method stub
    DialogFragment f = new DialogFragment();
    subject = subj;
    Log.v(TAG, subject.size()+"");
    return f;
}

to

public static ShowLstDialog newInstance(ArrayList<SubjSchedule> subj) {
    // TODO Auto-generated method stub
    ShowLstDialog f = new ShowLstDialog();
    subject = subj;
    Log.v(TAG, subject.size()+"");
    return f;
}

And do read

http://developer.android.com/reference/android/app/DialogFragment.html




回答2:


Instead of (or in addition to) implementing onCreateView(LayoutInflater, ViewGroup, Bundle) to generate the view hierarchy inside of a dialog, you may implement onCreateDialog(Bundle) to create your own custom Dialog object

So you should use:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)

to replace:

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



回答3:


Instead of

public static DialogFragment newInstance(...) {
    DialogFragment f = new DialogFragment();
    return f;
}

write

public static DialogFragment newInstance(...) {
    DialogFragment f = new ShowLstDialog();
    return f;
}

and also use Bundle and setArguments, getArguments to pass arguments.



来源:https://stackoverflow.com/questions/28715546/oncreateview-not-called-in-a-dialogfragment-from-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!