由于firebase dabase的异步性质,无法从firebase数据库检索数据

时间:2017-05-28 07:30:09

标签: android asynchronous arraylist firebase-realtime-database android-recyclerview

My ArrayList由于firebase API的异步性质,我第一次尝试检索数据时似乎是空的,第二次尝试加载数据它正确加载,这里是相同的代码。 用于检索数据的方法

    AdvertisementRef.orderByChild("Category").equalTo(categoryS).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) {
            System.out.println("The read failed: " + databaseError.getMessage());
        }
    });


    return adinfolist;

}

MainActivity

     rv.setLayoutManager(new LinearLayoutManager(this));
    rv.setHasFixedSize(true);
    advertismentref= FirebaseDatabase.getInstance().getReference("ADVERTISEMENT INFORMATION");
    helper=new Firebase_helper(advertismentref);
    adapter=new AdvertisementAdapter(helper.retrieve(/*catS*/),this);
    // adapter  = new AdvertisementAdapter(this, helper.retrieve());
    rv.setAdapter(adapter);

ViewHolder和适配器类

public class AdvertisementAdapter extends RecyclerView.Adapter <AdvertisementAdapter.AdvetisementViewHolder>{
ArrayList<SaveAddInformation>saveAddInformations=new ArrayList<SaveAddInformation>();
Context ctx;

public AdvertisementAdapter(ArrayList<SaveAddInformation> saveAddInformations,Context ctx) {
    this.saveAddInformations = saveAddInformations;
    this.ctx=ctx;
}

@Override
public AdvertisementAdapter.AdvetisementViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.design_row,parent,false);
    AdvetisementViewHolder myViewholder=new AdvetisementViewHolder(v,ctx,saveAddInformations);

    return myViewholder;
}

@Override
public void onBindViewHolder(AdvertisementAdapter.AdvetisementViewHolder holder, int position) {

    SaveAddInformation save=saveAddInformations.get(position);
    String title=save.getTitleS();
    holder.titleTv.setText(title);
    String rentamt=save.getRent_amount();
    holder.rentamt.setText(rentamt);
    String rentdays=save.getRentDaysSpin();
    holder.rentdays.setText(rentdays);
    String description=save.getProductDetails();
    holder.desc.setText(description);
    String imguri=save.getImageuri();

    holder.setImage(getApplicationContext(), imguri);



}



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


public static class AdvetisementViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView titleTv,rentamt,rentdays,desc;
    Context ctx;
    ImageView adimg;
    ArrayList<SaveAddInformation>saveAddInformations=new ArrayList<SaveAddInformation>();

    public AdvetisementViewHolder(View itemView,Context ctx,ArrayList<SaveAddInformation>saveAddInformations) {


        super(itemView);
        this.saveAddInformations=saveAddInformations;
        itemView.setOnClickListener(this);
        this.ctx=ctx;


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

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

        // We Need TO pass Context

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

    }

1 个答案:

答案 0 :(得分:2)

我刚刚将retrieve方法移动到main活动中,并调用了adapter.notifyDataSetChanged();在onDataChange和onCancelled方法,它工作!!!