0

我正在開發一個應用程序,它需要我從一個卡片視圖中的兩個Firebase數據庫引用中獲取引用。下面是cardview從一個卡片獲取多個Firebase數據庫引用的值查看

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp" 
    android:layout_marginRight="35dp" 
    android:layout_marginLeft="35dp" 
    android:layout_marginBottom="60dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="75dp" 
     > 
     <ImageView 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="5dp" 
      android:layout_marginTop="5dp" 
      android:layout_marginLeft="5dp" 
      app:srcCompat="@drawable/action_setup" 
      android:id="@+id/dPicture"/> 
     <TextView 
      android:text="Username" 
      android:layout_width="0dp" 
      android:layout_weight="4" 
      android:layout_height="match_parent" 
      android:gravity="center_vertical" 
      android:id="@+id/posted_username" 
      android:paddingLeft="15dp"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:elevation="10dp"> 


     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/posted_image" 
      android:scaleType="centerCrop" 
      android:adjustViewBounds="true" 
      android:src="@color/Orange" 
      /> 

     <TextView 
      android:text="Post Title..." 
      android:padding="15dp" 
      android:textSize="16dp" 
      android:textStyle="bold" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/posted_title"/> 

     <TextView 
      android:text="Summary..." 
      android:paddingLeft="15dp" 
      android:paddingRight="15dp" 
      android:paddingBottom="15dp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:maxLength="300" 
      android:maxLines="5" 
      android:id="@+id/posted_sharing"/> 



    </LinearLayout> 

</LinearLayout> 
</android.support.v7.widget.CardView> 

的模型,這是數據庫看起來怎麼樣在火力地堡

{ 
    "Users" : { 
    "1" : { 
     "image" : "http://something.com/something", 
     "name" : "person 1" 
    }, 
    "2" : { 
     "image" : "http://someone.com/someone", 
     "name" : "person 2" 
    } 
    }, 
    "posts" : { 
    "post 1" : { 
     "content" : "test content 1", 
     "dPicture" : "http://something.com/something", 
     "picture" : "http://postimage.com/postimage", 
     "title" : "test title 1", 
     "uid" : 1, 
     "username" : "person 1" 
    }, 
    "post 2" : { 
     "content" : "test content 2", 
     "dPicture" : "http://someone.com/someone", 
     "picture" : "http://postimage2.com/postimage2", 
     "title" : "test title 2", 
     "uid" : 2, 
     "username" : "person 2" 
    } 
    } 
} 

以及活動代碼

public class MainActivity extends AppCompatActivity { 

    private RecyclerView mPostedList; 
    private LinearLayoutManager mLayoutManager; 
    private DatabaseReference mDatabaseSharings; 
    private FirebaseAuth mAuth; 
    private FirebaseUser mCurrentUser; 
    private FirebaseAuth.AuthStateListener mAuthListener; 
    private DatabaseReference mDatabaseUsers; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     mAuth = FirebaseAuth.getInstance(); 
     mDatabaseSharings = FirebaseDatabase.getInstance().getReference().child("posts"); 
     mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users"); 
     mCurrentUser = mAuth.getCurrentUser(); 
     mDatabaseSharings.keepSynced(true); 

final String uid = mCurrentUser.getUid(); 


     //bring user to login activity when auth-state is null 

     mAuthListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
       if (firebaseAuth.getCurrentUser() == null) { 
        Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class); 
        loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(loginIntent); 
       } 
      } 
     }; 




//keep users logged in 
     mDatabaseUsers.keepSynced(true); 

     mPostedList = (RecyclerView) findViewById(R.id.posted_list); 
     mPostedList.setHasFixedSize(true); 
     mPostedList.setLayoutManager(new LinearLayoutManager(this)); 

    } 


    @Override 
    protected void onStart() { 
     super.onStart(); 


     mAuth.addAuthStateListener(mAuthListener); 

     //your adapter 
     FirebaseRecyclerAdapter<PostedModel, PostedModelViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PostedModel, PostedModelViewHolder>(

       PostedModel.class, R.layout.posted_row, PostedModelViewHolder.class, mDatabaseSharings 
     ) { 
      @Override 
      protected void populateViewHolder(PostedModelViewHolder viewHolder, PostedModel model, int position) { 


         viewHolder.setTitle(model.getTitle()); 
         viewHolder.setDPicture(getApplicationContext(), model.getDPicture()); 
         viewHolder.setSummary(model.getSummary()); 
         viewHolder.setImage(getApplicationContext(), model.getImage()); 
         viewHolder.setUsername(model.getUsername()); 


       final String post_key = getRef(position).getKey(); 





       viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() { 
        @Override 
        public boolean onLongClick(View view) { 

         Intent delete_intent = new Intent(MainActivity.this, DeleteActivity.class); 
         delete_intent.putExtra("blog_id", post_key); 
         startActivity(delete_intent); 
         return false; 
        } 
       }); 
       viewHolder.mView.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 

         Intent comment_intent = new Intent (MainActivity.this, CommentActivity.class); 
         comment_intent.putExtra("blog_id", post_key); 
         startActivity(comment_intent); 
        } 
       }); 


      } 
     }; 
     mPostedList.setAdapter(firebaseRecyclerAdapter); 
     mLayoutManager = new LinearLayoutManager(MainActivity.this); 
     mLayoutManager.setReverseLayout(true); 
     mLayoutManager.setStackFromEnd(true); 
     mPostedList.setLayoutManager(mLayoutManager); 


    } 

    public static class PostedModelViewHolder extends RecyclerView.ViewHolder{ 



     View mView; 

     public PostedModelViewHolder(View itemView) { 
      super(itemView); 





      mView = itemView; 
     } 

     public void setTitle(String title){ 


    TextView post_title = (TextView)mView.findViewById(R.id.posted_title); 
      post_title.setText(title); 
     } 
     public void setDPicture(Context ctx, String dPicture){ 

      ImageView post_dPicture = (ImageView) mView.findViewById(R.id.dPicture); 
      Picasso.with(ctx).load(dPicture).into(post_dPicture); 
     } 


     public void setSummary (String summary){ 

      TextView post_summary = (TextView)mView.findViewById(R.id.posted_sharing); 
      post_summary.setText(summary); 
     } 
     public void setImage(Context ctx, String image){ 
      ImageView post_image = (ImageView) mView.findViewById(R.id.posted_image); 
      Picasso.with(ctx).load(image).into(post_image); 


     } 
     public void setUsername (String username){ 
      TextView post_username = (TextView)mView.findViewById(R.id.posted_username); 
      post_username.setText(username); 
     } 

    } 


} 

當然還有就是PostedModel。類是它的吸氣和吸氣類

public class PostedModel { 
    private String title; 
    private String summary; 
    private String image; 
    private String dPicture; 


    private String username; 

    public PostedModel(){} 

    public PostedModel(String title, String summary, String image) { 
     this.title = title; 
     this.summary = summary; 
     this.image = image; 
     this.dPicture = dPicture; 
     this.username = username; 
    } 
    public String getDPicture() { 
     return dPicture; 
    } 

    public void setDPicture(String dPicture) {this.dPicture = dPicture;} 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) {this.title = title;} 

    public String getSummary() { 
     return summary; 
    } 

    public void setSummary(String summary) { 
     this.summary = summary; 
    } 

    public String getImage() { 
     return image; 
    } 

    public void setImage(String image) { 
     this.image = image; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

} 

現在,您可以看到我有一個發佈活動,它將「用戶」數據庫的用戶名以及圖像值傳遞給「posts」數據庫子(dPicture)。但是,當用戶更新他/她的用戶名時,只有「用戶」數據庫中的值發生變化,而不是從現有的「帖子」中更改。

我認爲最有效的方法是使用數據填充視圖這兩個數據庫引用,連接使用用戶的唯一標識,這是「用戶」數據庫中的關鍵和作爲子(uid)在「posts」數據庫中,但我不知道該怎麼做...我附上與盒數據庫的截圖快照的情況下,說明我很迷惑人跟我欠佳的技術語言

here

因此我們的目標是讓紅色盒裝的數據和它配對的Wi第三個綠色盒裝數據。

非常感謝所有幫助事先!

編輯: 我不知道如果我在正確的軌道與此......就這麼研究後,我認爲這是使用valueEventListener爲我想要得到數據的數據庫做的正確方法從適配器類的內部...

到目前爲止,我得到這個

FirebaseRecyclerAdapter<PostedModel, PostedModelViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PostedModel, PostedModelViewHolder>(

       PostedModel.class, R.layout.posted_row, PostedModelViewHolder.class, mDatabaseSharings 
     ) { 
      //calling the value from the Summary.java class 
      @Override 
      protected void populateViewHolder(final PostedModelViewHolder viewHolder, final PostedModel model, final int position) { 
mDatabaseUsers.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     if(dataSnapshot.getKey().toString() == mDatabaseSharings.child("uid").toString()){ 
     viewHolder.setUsername(model.getUsername()); 
     viewHolder.setDPicture(getApplicationContext(), model.getDPicture()); 
    } 

     viewHolder.setTitle(model.getTitle()); 
     viewHolder.setSummary(model.getSummary()); 
     viewHolder.setImage(getApplicationContext(), model.getImage()); 

    } 


    @Override 
    public void onCancelled(DatabaseError databaseError) { 

    } 

}); 

但我拿到鑰匙,而不是值傳遞哈哈

回答

0

嘗試生成兩個數組列表說ArrayL ist abc,def。然後,在您從用戶節點獲取數據後,請寫入分配用戶名和圖像,如abc.username。在此之後,從Posts節點獲取數據併爲其餘數據執行相同的操作。然後在兩個函數的最後,寫下def.add(abc);我認爲這應該能夠完成這項工作。

+0

嗨@Ambuj感謝您的評論。嗯,我認爲我應該使用valueEventListener,因爲這似乎是每個人都這樣做...我只是不知道如何正確實施它... – Jagmaster

相關問題