-1
我試圖從firebase數據庫檢索數據,當我嘗試在安裝應用程序後第一次檢索數據時,什麼都沒有顯示在回收站視圖中,但是當我再次按回時嘗試檢索數據,其正常顯示,建議我改變了我應該在我的代碼做,這樣的數據在第一次嘗試來自firebase數據庫的數據沒有正確顯示
方法用來返回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類
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類
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);
}
Firebase異步加載數據。在你迴歸'adinfolist'時,尚未填充。請參閱http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener –
其實我是新的android開發,請你詳細說明你的意思是說我的代碼。 –
@PriyankaKadam我的回答沒有幫助你? –