数据被添加到列表中,而不是在addChildEventListener的onChildChanged()中更新 [英] Data is getting added into list instead of getting updated in addChildEventListener's onChildChanged()
问题描述
我在firebase中有一些数据在某些特定操作上发生了变化,我希望在应用程序中显示更改。
我有一个<$ c $在这里代码:
$ b $ (){$ b pre $ databaseReference.child(uListings)。child(AccessToken.getCurrentAccessToken()。getUserId())。addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){
// recyclerview get populate here
}
$ b $ @Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){
if(dataSnapshot.getValue()!= null){
Map< String,String> map =(Map< String,String>)dataSnapshot.getValue();
Map< ; ArrayList< String>,ArrayList< String>> map2 =(Ma P< ArrayList的<字符串>,&ArrayList的LT;字符串>>)dataSnapshot.getValue();
String pDescription = map.get(pDescription);
String pDuration = map.get(pDuration);
String pPrice = map.get(pPrice);
String postedAt = map.get(postedAt);
String views = map.get(views);
ArrayList< String> imageUrl = map2.get(imageUrl); (imageUrl!= null){
UHandler pHandler = new UHandler(imageUrl.get(0),pDescription,pDuration,pPrice,postedAt,views);
list.add(pHandler);
adapter.notifyDataSetChanged();
progressBar.setVisibility(View.INVISIBLE);
} else {
Toast.makeText(getBaseContext(),imageUrlNone,Toast.LENGTH_SHORT).show();
}
} else {
Log.d(PDPchange,NULL);
progressBar.setVisibility(View.INVISIBLE);
$ b $覆盖
public void onChildRemoved(DataSnapshot dataSnapshot){
}
@覆盖
public void onChildMoved(DataSnapshot dataSnapshot,String s){
$ b @Override $ b $ public void onCancelled(DatabaseError databaseError){
}
});
这里是 UHandlerAdapter.java
:
public class UHandlerAdapter extends RecyclerView.Adapter< UHandlerAdapter.MyViewHolder> {
private Context mContext;
私人列表< UHandler> listingList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView pDescription,pDuration,pPrice,postedAt,listingViews,imageUrl;
public ArrayList< String> imageUrls;
public CircleImageView pImage;
$ b $ public MyViewHolder(View view){
super(view);
pImage =(CircleImageView)view.findViewById(R.id.p_image_profile);
pDescription =(TextView)view.findViewById(R.id.p_description_profile);
pDuration =(TextView)view.findViewById(R.id.p_duration_profile);
pPrice =(TextView)view.findViewById(R.id.p_price_profile);
postedAt =(TextView)view.findViewById(R.id.p_posted_when_profile);
listingViews =(TextView)view.findViewById(R.id.listing_views_profile);
imageUrl =(TextView)view.findViewById(R.id.image_urls_profile);
$ b public UHandlerAdapter(Context mContext,List< UProfileHandler> listingList){
this.mContext = mContext;
this.listingList = listingList;
$ b @Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.profile_all,parent,false);
返回新的MyViewHolder(itemView);
$ b @Override
public void onBindViewHolder(final MyViewHolder holder,int position){
UHandler handler = listingList.get(position);
holder.pDescription.setText(handler.getPDescriptionProfile());
holder.pDuration.setText(handler.getPDurationProfile());
holder.pPrice.setText(handler.getPPriceProfile());
holder.postedAt.setText(handler.getPostedAt());
holder.listingViews.setText(handler.getListingViews());
//使用Glide库
加载专辑封面Glide.with(mContext)
.load(handler.getPImageProfile())
.apply(new RequestOptions() .placeholder(R.drawable.ic_placeholder).error(R.drawable.ic_error))
.into(holder.pImage);
$ b @Override
public int getItemCount(){
return listingList.size();
$ p $问题是在 onChildChanged()
获取更改的视图
在列表中添加一个完整的其他项目,反映它已更改的值,而不仅仅是以前添加的项目 onChildAdded()
。
如何才能在之前添加的项目中更新?
$您可以存储键
以更新它,例如: $ b 解决方案
$ b ArrayList< String> mKeys = new ArrayList< String>();
$ b $ databaseReference.child(uListings)。child(AccessToken.getCurrentAccessToken()。getUserId())。addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded (DataSnapshot dataSnapshot,String s){
// recyclerview在此填充
String key = dataSnapshot.getKey();
mKeys.add(key);
adapter.notifyDataSetChanged();
$ b @Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){
if(dataSnapshot.getValue )!= null){
.......
//现在我们得到索引来更新特定的值
String key = dataSnapshot.getKey();
int index = mKeys.i ndexOf(键);
list.set(index,pHandler);
adapter.notifyDataSetChanged();
....
}
I have some data in firebase which gets changed on some specific actions and I want that change to be shown in the app.
I have a RecyclerView
in which all the data from firebase is getting populated.
Here's code:
databaseReference.child("uListings").child(AccessToken.getCurrentAccessToken().getUserId()).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// recyclerview gets populated here
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
Map<String,String> map = (Map<String,String>)dataSnapshot.getValue();
Map<ArrayList<String>,ArrayList<String>> map2 = (Map<ArrayList<String>,ArrayList<String>>)dataSnapshot.getValue();
String pDescription = map.get("pDescription");
String pDuration = map.get("pDuration");
String pPrice = map.get("pPrice");
String postedAt = map.get("postedAt");
String views = map.get("views");
ArrayList<String> imageUrl = map2.get("imageUrl");
if (imageUrl != null) {
UHandler pHandler = new UHandler(imageUrl.get(0), pDescription, pDuration, pPrice, postedAt, views);
list.add(pHandler);
adapter.notifyDataSetChanged();
progressBar.setVisibility(View.INVISIBLE);
} else {
Toast.makeText(getBaseContext(), "imageUrlNone", Toast.LENGTH_SHORT).show();
}
} else {
Log.d("PDPchange", "NULL");
progressBar.setVisibility(View.INVISIBLE);
}
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here's UHandlerAdapter.java
:
public class UHandlerAdapter extends RecyclerView.Adapter<UHandlerAdapter.MyViewHolder> {
private Context mContext;
private List<UHandler> listingList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView pDescription, pDuration, pPrice, postedAt, listingViews, imageUrl;
public ArrayList<String> imageUrls;
public CircleImageView pImage;
public MyViewHolder(View view) {
super(view);
pImage = (CircleImageView) view.findViewById(R.id.p_image_profile);
pDescription = (TextView) view.findViewById(R.id.p_description_profile);
pDuration = (TextView) view.findViewById(R.id.p_duration_profile);
pPrice = (TextView) view.findViewById(R.id.p_price_profile);
postedAt = (TextView) view.findViewById(R.id.p_posted_when_profile);
listingViews = (TextView) view.findViewById(R.id.listing_views_profile);
imageUrl = (TextView) view.findViewById(R.id.image_urls_profile);
}
}
public UHandlerAdapter(Context mContext, List<UProfileHandler> listingList) {
this.mContext = mContext;
this.listingList = listingList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.profile_all, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
UHandler handler = listingList.get(position);
holder.pDescription.setText(handler.getPDescriptionProfile());
holder.pDuration.setText(handler.getPDurationProfile());
holder.pPrice.setText(handler.getPPriceProfile());
holder.postedAt.setText(handler.getPostedAt());
holder.listingViews.setText(handler.getListingViews());
// loading album cover using Glide library
Glide.with(mContext)
.load(handler.getPImageProfile())
.apply(new RequestOptions().placeholder(R.drawable.ic_placeholder).error(R.drawable.ic_error))
.into(holder.pImage);
}
@Override
public int getItemCount() {
return listingList.size();
}
}
The problem is that in onChildChanged()
the views
which gets changed add a complete other item in the list reflecting it's changed value and not just gets updated in previously added items in onChildAdded()
.
How can I just update it in the previously added items itself?
解决方案 You can store keys
in order to update it e.g:
ArrayList<String> mKeys = new ArrayList<String>();
databaseReference.child("uListings").child(AccessToken.getCurrentAccessToken().getUserId()).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// recyclerview gets populated here
String key = dataSnapshot.getKey();
mKeys.add(key);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
.......
//now we get the index to update specific value
String key = dataSnapshot.getKey();
int index = mKeys.indexOf(key);
list.set(index, pHandler);
adapter.notifyDataSetChanged();
....
}
这篇关于数据被添加到列表中,而不是在addChildEventListener的onChildChanged()中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文