Android使用gridview和realm DB创建动态数据动态片段?

时间:2016-03-22 17:11:26

标签: android android-fragments android-gridview realm

我想加载类别和产品,如图所示: 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;
    }
}

使用newInstance和不同的数据创建片段。

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

}

它加载会创建多个片段作为SubCategoriesCount的值。但是所有片段中的数据都是相同的。意味着所有片段上的所有gridView都具有相同的网格数据。

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

CategoryName和ID显示..但数据不会改变... 它可能是Realm数据。如何处理不同活动的领域处理。

1 个答案:

答案 0 :(得分:0)

领域中有重复的数据。所以上面的代码没有问题。我没有注意到realm数据库的内容。