-1

I am trying to retrieve data from firebase database, when i try to retrieve data for the very first time after installing the app nothing is displayed in recycler view but when i press back and once again try to retrieve data,its displayed properly, suggest me changed which i should do in my code so that data is properly displayed in first attempt

Method Used to return ArrayList

 public ArrayList<SaveAddInformation> retrieve(){

    AdvertisementRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {

                SaveAddInformation mModel = eventSnapshot.getValue(SaveAddInformation.class);
                Log.d("DATA" ,""+ mModel);
                adinfolist.add(mModel);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    return adinfolist;

}

Adpater Class

 Context c;
ArrayList<SaveAddInformation> adinfolist;

public MyAdapter(Context c, ArrayList<SaveAddInformation> adinfolist) {
    this.c = c;
    this.adinfolist = adinfolist;
}

@Override
public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(c).inflate(R.layout.design_row,parent,false);
  MyViewholder myViewholder=new MyViewholder(v);

   return myViewholder;
}

@Override
public void onBindViewHolder(MyViewholder holder, int position) {
    SaveAddInformation save=adinfolist.get(position);
    holder.titleTv.setText(save.getTitleS());
    holder.rentamt.setText(save.getRent_amount());
    holder.rentdays.setText(save.getRentDayS());
    holder.desc.setText(save.getDescriptionS());

    holder.setImage(getApplicationContext(), save.getImageuri());

  //  holder.titleTv.setText((CharSequence) adinfolist.get(position));
}

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

}

ViewHolder Class

 public MyViewholder(View itemView) {
    super(itemView);
    titleTv=(TextView)itemView.findViewById(R.id.Titletextv);

    rentamt=(TextView)itemView.findViewById(R.id.rent_amount);
    rentdays=(TextView)itemView.findViewById(R.id.days_rent);
    desc=(TextView)itemView.findViewById(R.id.descTV);




}

public void setImage(Context applicationContext, String imageuri) {

    adimg=(ImageView)itemView.findViewById(R.id.ad_image);

    // We Need TO pass Context

    Picasso.with(applicationContext).load(imageuri).into(adimg);



}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

If you are sure that the code has no errors, I would like to remind you that Firebase connections are async. So the first time the function retrieve() returns the value of arrayList is actually empty. What you can do is call adapter.notifyDataSetChanged() after the for loop in onDataChange() method.

sanidhya pal
  • 345
  • 1
  • 9