【问题标题】:OnCreateView not called in a dialogfragment from a fragmentOnCreateView 未在来自片段的对话框片段中调用
【发布时间】:2015-02-25 09:36:44
【问题描述】:

我有一个片段,一旦单击按钮,就会执行或显示对话框片段。此对话框片段显示由代码生成的表,但是当我尝试单击此按钮时显示

当我在 LogCat 中跟踪它时,不会调用对话框片段 onCreateView。有人可以帮我解释一下吗?我在 android 编程方面还不是很好,我知道我还有很多东西要学。

这里是调用对话框片段的片段代码

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");
        }

    });

   ..........

这是我的对话片段

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;
}



}

这里是对话框片段的xml文件

  <?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>

非常感谢您。

【问题讨论】:

  • 把这个DialogFragment f = new DialogFragment();改成ShowLstDialog f = new ShowLstDialog ()
  • @Raghunandan,你为什么不把它作为答案发布?

标签: android android-fragments android-dialogfragment


【解决方案1】:

改变这个

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;
}

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;
}

请阅读

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

【讨论】:

    【解决方案2】:

    您可以实现 onCreateDialog(Bundle) 来创建自己的自定义 Dialog 对象,而不是(或除了)实现 onCreateView(LayoutInflater, ViewGroup, Bundle) 来生成对话框内的视图层次结构

    所以你应该使用:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    

    替换:

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

    【讨论】:

    【解决方案3】:

    代替

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

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

    还可以使用 Bundle 和 setArguments、getArguments 来传递参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多