Search code examples
androidandroid-fragmentsandroid-gridviewrealm

Android creating dynamic fragments with dynamic data using gridview and realm DB?


I want to load category and products like the image shown: frag enter image description here

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Category ctID = mSubCategory.get(position);
        int id = ctID.getCategory_id();
        return PlaceholderFragment.newInstance(getBaseContext(),id);
    }

    @Override
    public int getCount() {
        // Set number of fragments to be created
        if(SubCategoriesCount == 0) {
            Category ct;
            ct = mSubCategory.get(0);
            if (ct != null)
                return 1;
        }
        return SubCategoriesCount;
    }
}

Creating Fragment using newInstance with different Data.

PlaceholderFragment.java

public class PlaceholderFragment extends Fragment {

private static final String FragmentCategoryID = "CategoryID";
private static Context cTx;
private static String catName;
public PlaceholderFragment() {
}

public static PlaceholderFragment newInstance(Context ctx, int id){
    PlaceholderFragment fragment = new PlaceholderFragment();
    cTx = ctx;
    Log.d("New Fragment Created ", ":" + id );
    Bundle args = new Bundle();
    args.putInt(FragmentCategoryID, id);
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_category_products_view, container, false);

    RealmResults<Product> ProductList;
    ProductList = GetProducts(getArguments().getInt(FragmentCategoryID));
    Log.d("Fragment Arguments", "" + getArguments().getInt(FragmentCategoryID));

    GridView gv = (GridView) rootView.findViewById(R.id.gridViewFrg);

    gv.setAdapter(new AdapterRealmProduct(getActivity(), R.layout.grid_view_main, ProductList, true));

    TextView textView = (TextView) rootView.findViewById(R.id.section_label);

    textView.setText(getString(R.string.section_format, getArguments().getInt(FragmentCategoryID))+ ":::"+catName);

    return rootView;
}

private RealmResults<Product> GetProducts(int CategoryID){

    RealmConfiguration realmConfigs;
    realmConfigs = new RealmConfiguration.Builder(getActivity()).build();
    Realm realmtm = Realm.getInstance(realmConfigs);
    Category camt = realmtm.where(Category.class).equalTo("category_id", CategoryID).findFirst();
    catName = camt.getName();
    RealmResults<Product> results = realmtm.where(Product.class).equalTo("category_id", CategoryID).findAll();
    try{
    if(results != null && results.isValid()) {
        return results;
    }
    }catch (IllegalStateException e){
        e.printStackTrace();
    }finally {
        //realmtm.close();
    }
    return results;
}

}

It loads creates multiple fragments as value of SubCategoriesCount. But data in all fragments is same. Means All gridViews on all fragments have same grid data.

    textView.setText(getString(R.string.section_format, getArguments().getInt(FragmentCategoryID))+ ":::"+catName);

CategoryName and ID displays.. but data does not change... it might be Realm data. how to handle realm handle in different activitis.


Solution

  • there was repeated data in realm. So there is no problem in above code. I didn't notice what is coming to realm database.