Android 使用 gridview 和 realm DB 使用动态数据创建动态片段?
Android creating dynamic fragments with dynamic data using gridview and 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;
}
}
使用具有不同数据的 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 显示..但数据没有改变...
它可能是领域数据。如何在不同的活动中处理领域句柄。
领域中有重复数据。所以上面的代码没有问题。我没有注意到领域数据库会发生什么。
我想加载如图所示的类别和产品:
碎片
/**
* 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 显示..但数据没有改变... 它可能是领域数据。如何在不同的活动中处理领域句柄。
领域中有重复数据。所以上面的代码没有问题。我没有注意到领域数据库会发生什么。