1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
private static final String TAG = MainActivity.class.getSimpleName();
private GridView gridView;
private List<CategoryEntity> categoryEntityList;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categoryEntityList = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("category");
gridView = findViewById(R.id.grid_view);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//send the selected category id to next activity
Bundle b = new Bundle();
b.putParcelable("category", categoryEntityList.get(i));
Intent intent = new Intent(getApplicationContext(), ListSelonCategorie.class);
intent.putExtras(b);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
categoryEntityList.clear();
for (DataSnapshot categorySnapShot : dataSnapshot.getChildren()) {
Log.d(TAG, "Outside : " + categorySnapShot.child("cat_id").getValue());
CategoryEntity categoryEntity = new CategoryEntity();
categoryEntity.setCat_id(categorySnapShot.child("cat_id").getValue(Long.class));
categoryEntity.setCat_name(categorySnapShot.child("cat_name").getValue(String.class));
categoryEntity.setCat_image(categorySnapShot.child("cat_image").getValue(String.class));
List<ProduitEntity> produitEntityList = new ArrayList<>();
for (DataSnapshot productSnapShot : categorySnapShot.child("products").getChildren()) {
ProduitEntity produitEntity = new ProduitEntity();
produitEntity.setPd_id(productSnapShot.child("pd_id").getValue(Long.class));
produitEntity.setPd_desc(productSnapShot.child("pd_desc").getValue(String.class));
produitEntity.setPd_image(productSnapShot.child("pd_image").getValue(String.class));
produitEntity.setPd_name(productSnapShot.child("pd_name").getValue(String.class));
produitEntity.setPd_prix(productSnapShot.child("pd_prix").getValue(Double.class));
produitEntityList.add(produitEntity);
}
categoryEntity.setProducts(produitEntityList);
Log.d(TAG, "Categroy Data : " + categoryEntity.toString());
categoryEntityList.add(categoryEntity);
}
populateGridView();
} |
Partager