Search code examples
androidgridviewdialogandroid-dialogfragmentdialogfragment

Dialog with Gridview sometimes doesn't load


I'm making an app that makes use of a GridView to display subcategories. I have the categories displayed on screen and when clicked a dialog appears with a GridView displaying subcategories and a button to view everything in that category.

Here's the problem: Sometimes the GridView doesn't load on the first click, I have to close it and then click on the category again for it to open. Categories are basically images with a textview on top. Pictures are loaded from the Internet.

Code for Dialog:

public class dialogFragment extends android.support.v4.app.DialogFragment {

    private Button mViewAll;
    private GridView mSubCatsView;
    public ArrayList<category> mCats = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_fragment, container);
        mCats.addAll(((shopActivity)getActivity()).mDialogCats);
        mViewAll = v.findViewById(R.id.viewAllBTN);
        mSubCatsView = v.findViewById(R.id.subCatsView);
        final gridAdapter adapada = new gridAdapter(getActivity(), mCats);
        mSubCatsView.setAdapter(adapada);
        mSubCatsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ArrayList<category> subSubCats = new ArrayList<>();
                for (int y = 0; y < ((shopActivity)getActivity()).mCategoriesAll.size(); y++){
                    if(mCats.get(i).getId() == ((shopActivity)getActivity()).mCategoriesAll.get(y).getParent()){
                        subSubCats.add(((shopActivity)getActivity()).mCategoriesAll.get(y));
                    }
                }
                if(subSubCats.size() > 0){
                    mCats.clear();
                    mCats.addAll(subSubCats);
                    adapada.notifyDataSetChanged();
                }
            }
        });
        mViewAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return v;
    }
}

Solution

  • If anyone else has this problem, I fixed it by using a RecyclerView + GridLayoutManager to mimic a GridView.