Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – When i retrive children from firebase using recylceview TextView Won’t Show

Posted on November 11, 2022 By

Solution 1 :

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

Problem :

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) {

            }
        });


    }

Comments

Comment posted by Horalx SAx

Thank you so much, I don’t know how did I miss that

READ  [FIXED] android - How to sendMultiPartTextMessage in Java
Powered by Inline Related Posts

Android Tags:android, firebase, firebase-realtime-database, google-cloud-platform, java

Post navigation

Previous Post: [FIXED] android developer api – there is a ‘Late Initialization Error’ during flutter development
Next Post: [FIXED] android – How to set SSL pinning using retrofit for Firebase Realtime Database Url?

Related Posts

[FIXED] android – How to find the default style of EditText? Android
[FIXED] android – How to run worker manager even after the app has been destroyed Android
[FIXED] java – Invalid GmsCore APK, remote loading disabledrequires the Google Play Store but it is missing Firebase object access showing null pointer exception Android
[FIXED] dialogflow es – Is it possible to use same training phrases in different intents? Android
[FIXED] android – Reading pictures from the storage Android
[FIXED] android – can i post an app that contains only ads, on google play store Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme