DialogFragment中的自定义gridview

时间:2014-08-22 16:17:19

标签: android android-fragments android-gridview baseadapter android-dialogfragment

我尝试将自定义gridview插入DialogFragment,我遇到了将适配器设置为gridview的问题。经过几个小时的研究,我放弃了来到这里 有关gridview.setAdapter问题的任何想法? 提前谢谢!

DialogFragment:

public class theDialog extends DialogFragment {

private String[] nums ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
GridView gridview;
GridAdapter adapter = new GridAdapter(getActivity(),nums);

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    View view=inflater.inflate(R.layout.grid,null);
    gridview = (GridView) getActivity().findViewById(R.id.gridview);
    gridview.setAdapter(adapter); //PROBLEM HERE************

    builder.setView(view);


    Dialog dialog = builder.create();
    return dialog;
}
}

1 个答案:

答案 0 :(得分:0)

首先,如果您在运行代码时收到任何错误,则应该在您的问题中编辑logcat的输出。

要解决您的问题,您应该使用已膨胀而不是view的{​​{1}}来查找getActivity

固定的DialogFragment:

gridview