Skip to content
Advertisement

When i retrive children from firebase using recylceview TextView Won’t Show

I’m using Android Studio when I run my app and add or show RecycleView list, text view shows only the title not data inside firebase this is the textviewaddCateogryclass What would be causing this? used the same data on firebase child.

firebase realtime database Firebase

ModelCategoryClass

public class ModelCategoryClass {
    //same variable in db
    String id, category, uid;
    long timestamp;

    public ModelCategoryClass() {}

    public ModelCategoryClass(String id, String category, String uid, long timestamp) {
        this.id = id;
        this.category = category;
        this.uid = uid;
        this.timestamp = timestamp;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}

AdapterCategoryClass

public class AdapterCategoryClass extends RecyclerView.Adapter<AdapterCategoryClass.HolderCategory> {

    private Context context;
    private ArrayList<ModelCategoryClass> categoryClassArrayList;
    //xml rows_categorys
    private RowsCategorysBinding binding;




    //constucor
    public AdapterCategoryClass(Context context, ArrayList<ModelCategoryClass> categoryClassArrayList) {
        this.context = context;
        this.categoryClassArrayList = categoryClassArrayList;
    }





    ///////////////
    //generated from RecyclerView.Adapter<AdapterCategoryClass.HolderCategory>
    @NonNull
    @Override
    public HolderCategory onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //binding

        binding = RowsCategorysBinding.inflate(LayoutInflater.from(context), parent, false);
        return new HolderCategory(binding.getRoot());
        //return new HolderCategory(RowsCategorysBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false));
    }


    //getting data
    @Override
    public void onBindViewHolder(@NonNull HolderCategory holder, int position) {
        ModelCategoryClass model = categoryClassArrayList.get(position);

        String id = model.getId();
        String category = model.getId();
        String uid = model.getId();
        long timestamp = model.getTimestamp();


//remove btn
        //as we declred remove btn in HolderCatogory method
        holder.removeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,""+category,Toast.LENGTH_SHORT).show();



            }
        });

    }

    @Override
    public int getItemCount() {
        return categoryClassArrayList.size();
    }
/////////////







    //hold lists xml
    public class HolderCategory extends RecyclerView.ViewHolder {
        TextView textViewListCatt;
        ImageButton removeBtn;



        //constu
        public HolderCategory(@NonNull View itemView) {
            super(itemView);

            textViewListCatt = binding.textViewListCat;
            removeBtn = binding.removeBtn;

        }
    }



}

CategoryAddActivity class

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCategoryAddBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
//init firebase auth
        firebaseAuth = FirebaseAuth.getInstance();

        ShowCategories();
    private void ShowCategories() {

        categoryClassArrayList = new ArrayList<>();
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Categories");


        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
//clear array befor add data
                categoryClassArrayList.clear();
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    //get data from child in database
                    ModelCategoryClass mod = dataSnapshot.getValue(ModelCategoryClass.class);
                    //add data to array
                    categoryClassArrayList.add(mod);
                }
                //start using adapter
                adapterCategoryClass = new AdapterCategoryClass(CategoryAddActivity.this, categoryClassArrayList);
                //get recycleListview rom xml and set it with defualt set method
                binding.recycleList.setAdapter(adapterCategoryClass);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });


    }

Advertisement

Answer

The following lines of code:

ModelCategoryClass model = categoryClassArrayList.get(position);
String id = model.getId();
String category = model.getId();
String uid = model.getId();
long timestamp = model.getTimestamp();

Should be changed to:

ModelCategoryClass model = categoryClassArrayList.get(position);
String id = model.getId();
String category = model.getCategory(); //👈
String uid = model.getUid(); //👈
long timestamp = model.getTimestamp();

Meaning that you need to call the corresponding getters. But that’s not the entire problem. You aren’t displaying something because you aren’t setting those values to their corresponding views. To solve this, you have to create inside your HolderCategory class a method that actually binds the data to the views. For example, to set the category, you should use something like this:

void setCategory(String category) {
    binding.textViewListCat.setText(category);
}

And then call this method from inside your onBindViewHolder method like this:

holder.setCategory(category);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement