2016-03-22 7 views
0

Ich möchte wie das Bild Kategorie und Produkte laden gezeigt: frag enter image description hereAndroid erstellt dynamische Fragmente mit dynamischen Daten mit Gridview und Realm DB?

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

Erstellen Fragment newInstance Verwendung mit unterschiedlichen Daten.

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

}

Es mehrere Fragmente als Wert von SubCategoriesCount lädt erzeugt. Aber Daten in allen Fragmenten sind gleich. Mittel Alle GridViews auf allen Fragmenten haben dieselben Grid-Daten.

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

Kategorie und ID zeigt .. aber Daten nicht ändert ... es könnte Realm Daten sein. Wie handle ich Realm-Handle in verschiedenen Activitis.

Antwort

0

Es gab wiederholte Daten in Realm. Es gibt also kein Problem im obigen Code. Ich habe nicht bemerkt, was in die Realm-Datenbank kommt.