0

I want to fetch the data from firebase and add it to cardView. When I fetched title and description, title is displayed but my description isnt displayed.

This is my model class:

public class News {
    public String mtitle, mdesc;

    public News(){

    }

    public News(String title, String desc) {
        mtitle = title;
        mdesc = desc;
    }

    public String getTitle() {
        return mtitle;
    }

    public void setTitle(String title) {
        mtitle = title;
    }

    public String getDesc() {
        return mdesc;
    }

    public void setDesc(String desc) {
        mdesc = desc;
    }
}

This is my controller:

//FR options and adapter
    options = new FirebaseRecyclerOptions.Builder<News>()
            .setQuery(newsRef, News.class).build();

    adapter = new FirebaseRecyclerAdapter<News, NewsViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull NewsViewHolder holder, int position, @NonNull News model) {
            holder.mtitle.setText(model.getTitle());
            holder.mdesc.setText(model.getDesc());
        }

        @NonNull
        @Override
        public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_row, viewGroup, false);

            return new NewsViewHolder(view);
        }
    };

    GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 1);
    mNewsList.setLayoutManager(gridLayoutManager);
    adapter.startListening();
    mNewsList.setAdapter(adapter);

This is my firebase structure:

image

André Kool
  • 4,880
  • 12
  • 34
  • 44

1 Answers1

0

Have a look at @PropertyName("title") documentation. All-tough it is poorly documented, it indicates that you need this annotation to (de)serialize your model objects. See this question for more details

Basically, you can use it to match a property in Firebase to a field in your Model class. In your case it would become something like this (untested):

public class News {

    @PropertyName("title")
    public String mtitle;

    @PropertyName("description")
    public String mDesc;

    public News(String title, String desc) {
        mtitle = title;
        mdesc = desc;
    }
}

Or, when you switch to kotlin:

data class News(
    @set:PropertyName("title") var title: String = "",
    @set:PropertyName("description") var desc: String = "",
)
Entreco
  • 12,738
  • 8
  • 75
  • 95