在android片段中显示GridView

时间:2017-05-21 05:55:02

标签: android android-fragments gridview

CustomGrid是我的GridView适配器,MainGrid是我要在其中显示GridView的片段。我做到了这一点,但它没有用。我该怎么办?

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

            View view = inflater.inflate(R.layout.main_xml, container, false);

            CustomGrid adapter = new CustomGrid(MainGrid.this, web, imageId);

            GridView grid = (GridView) view.findViewById(R.id.grid);
            grid.setAdapter(adapter);

            toolbar = (Toolbar)view.findViewById(R.id.fab_toolbar);

            setupToolbar();

            return view;
        }

1 个答案:

答案 0 :(得分:0)

你做错了是,你正在向适配器发送片段的上下文,这是错误的。试试这个

Activity act;

onCreateView

act = getActivity();

然后

CustomGrid adapter = new CustomGrid(act, web, imageId);

希望这有帮助