0

I'm trying to make and activity that uses fragment with viewpager2 and I'm having problem populating the grid view inside the viewpage2. and I'm having trouble using adapters. what are the steps I need to know to make this activity work?

this is my fragment class

public class item_frag extends Fragment{

    Context context;
    FragmentItemDisplayBinding binding;
    String name, description, category, price, imageurl;
    FirebaseStorage firebaseStorage;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {



        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference("inventory");
        firebaseStorage = FirebaseStorage.getInstance();

        name = databaseReference.getDatabase().getReference("inventory").toString();
        description = databaseReference.getDatabase().getReference("inventory").toString();
        category = databaseReference.getDatabase().getReference("inventory").toString();
        price = databaseReference.getDatabase().getReference("inventory").toString();
        imageurl = firebaseStorage.getReference("inventory").toString();

        String[] itemname = {"hello"};
        int[] itemimage = {R.drawable.unimas_logo};

        View view = inflater.inflate(R.layout.fragment_item_display, container, false);
        GridView gridView = (GridView) view.findViewById(R.id.grid_view);
        item_frag_adapter itemFragAdapter = new item_frag_adapter(context, itemname, itemimage);
        gridView.setAdapter(itemFragAdapter);

        return inflater.inflate(R.layout.fragment_item_display, container, false);
    }

}

this is my item fragment adapter class

public class item_frag_adapter extends BaseAdapter {

    Context context;
    String[] itemname;
    int[] image;

    LayoutInflater inflater;

    public item_frag_adapter(Context context, String[] itemname, int[] image) {
        this.context = context;
        this.itemname = itemname;
        this.image = image;
    }

    @Override
    public int getCount() {
        return itemname.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null)
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null){

            convertView = inflater.inflate(R.layout.item_card,null);

        }

        ImageView imageView = convertView.findViewById(R.id.idIVimage);
        TextView textView = convertView.findViewById(R.id.idTVtext);

        imageView.setImageResource(image[position]);
        textView.setText(itemname[position]);

        return convertView;
    }
}
Bochiii
  • 13
  • 5

1 Answers1

0

I really want to thank all the contribution given to solve this problem.

I have found the solution to this error, so my first initial was the solution where I loop the the listener twice,

First loop to load every data inside the cart list(which are item ID) for the current user. Second loop I load everything inside the list using a childevent listener (the same code I use to load the main feed with everything inside the inventory of every user) but, this part I filter the second loop with matching ID on the first loop. to be safe I set the id and the value with the ID every time the add to cart.

It took a lot of debugging and I realized that my mistake was I during the first loop I took both the ID and Value of and place it in a holder so it looked like this : JH9asdh7asd:JH9asdh7asd instead of JH9asdh7asd

so there were never any match.

so the solution was to only take either the id or the value

cartref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

                String cartkey = snapshot.getValue(String.class);

                DatabaseReference itemRef = FirebaseDatabase.getInstance().getReference("User");
                itemRef.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                        for (DataSnapshot childSnapshot: snapshot.child("Inventory").getChildren()) {


                            CartInventory inventory = childSnapshot.getValue(CartInventory.class);
                            inventory.setKey(childSnapshot.getKey());
                            String itemKey = inventory.getKey();
                            if(itemKey.equals(cartkey)){
                                cartArrayList.add(inventory);
                            }
                        }
                        cartAdapter.notifyDataSetChanged();
                    }

         // the rest of the childevent listener 
Bochiii
  • 13
  • 5