0

我在firebase中有一些數據在某些特定操作上發生了更改,我希望在應用程序中顯示該更改。數據正在添加到列表中,而不是在addChildEventListener的onChildChanged()中更新

我有一個RecyclerView其中來自firebase的所有數據都被填充。

這裏是代碼:

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

      } 
     }); 

這裏的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(); 
    } 
} 

的問題是,在​​其中被改變了views在列表中反映它的變化值,而不是僅僅增加一個完整的其他項目在onChildAdded()的以前添加的項目中得到更新。

我怎樣才能在之前添加的項目中更新它呢?

+0

首先你必須用'ArrayList的 IMAGEURL = map2.get( 「IMAGEURL」)中的錯誤;''map.get(對象鍵)'不是類型安全的,即。編譯器不會抱怨,如果你給一個錯誤類型的鍵。有關更多信息,請參閱https://stackoverflow.com/questions/857420/what-are-the-reasons-why-map-getobject-key-is-not-fully-generic。 – alan7678

+0

@ alan7678沒關係。感謝您指出。請回答問題。 –

+0

假設'UHandler'是你的視圖模型,如果你不想要一個新的項目,你不應該使用'new'。因此,簡單地說,不要使用'new'不要使用'list.add',使用'list.get'來檢索你的對象並更新它。 – alan7678

回答

1

可以存儲keys以更新它例如:

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(); 
           .... 
        } 
+0

這工作!將在14小時後授予賞金(根據StackOverflow的提示)。 –

+0

你可以幫助這個:https://stackoverflow.com/q/46638945/6144372「:| –

+0

@HammadNasir我不知道如何圖書館工作,對不起。 – Ibrahim

相關問題