1

My ArrayList由於Firebase API的異步性質,第二次嘗試加載數據時,由於第一次嘗試檢索數據,似乎爲空它的加載正確,這裏是相同的代碼。 方法,其用於檢索數據由於Firebase數據庫的異步性質,無法從Firebase數據庫中檢索數據

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

    } 
+0

你可以使用接口相同,當你只是在更新適配器使用接口獲取數據 – Pavan

回答

2

我剛纔動議檢索方法到主活性和稱爲adapter.notifyDataSetChanged() ;在onDataChange和onCancelled方法,它的工作!

相關問題