2017-08-22 259 views
0

我試圖從在火力地堡Recyclerview我火力地堡的數據庫顯示,這是我的數據庫數據顯示火力地堡Retrive數據

enter image description here

,我嘗試使用相同的教程代碼

Firebase UI

我嘗試並獲得這個結果 enter image description here

我對Android和一般編程相當陌生,所以任何幫助,將不勝感激。這是相關的代碼。

PostlistFragment

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    mActivity = getActivity(); 

    final Dialog mDialog = new Dialog(mActivity, R.style.NewDialog); 
    mDialog.addContentView(
      new ProgressBar(mActivity), 
      new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT) 
    ); 
    mDialog.setCancelable(true); 
    mDialog.show(); 

    // Set up Layout Manager, reverse layout 
    LinearLayoutManager mManager = new LinearLayoutManager(mActivity); 
    mManager.setReverseLayout(true); 
    mManager.setStackFromEnd(true); 
    mRecycler.setLayoutManager(mManager); 

    // Set up FirebaseRecyclerAdapter with the Query 
    Query postsQuery = getQuery(mDatabase); 
    mAdapter = new FirebaseRecyclerAdapter<PostMainboard, MainboardViewHolder>(PostMainboard.class, R.layout.mainboard_list, MainboardViewHolder.class, postsQuery) { 
     @Override 
     public void onDataChanged() { 
      super.onDataChanged(); 
      mDialog.dismiss(); 
     } 

     @Override 
     protected void populateViewHolder(final MainboardViewHolder viewHolder, final PostMainboard model, final int position) { 
      final DatabaseReference postRef = getRef(position); 


      viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent(mActivity, MainboardDetailActivity.class); 
        intent.putExtra(MainboardDetailActivity.EXTRA_POST_KEY, postRef.getKey()); 
        startActivity(intent); 
       } 
      }); 
     } 
    }; 
    mRecycler.setAdapter(mAdapter); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if (mAdapter != null) { 
     mAdapter.cleanup(); 
    } 
} 
public abstract Query getQuery(DatabaseReference databaseReference); 

而且我詳細的活動的成果得到了同樣沒有顯示出每一個

enter image description here

這裏我詳細活動代碼

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view_mainboard_detail); 

    mBodyView = (TextView)findViewById(R.id.post_detail); 
    mAuthorView = (TextView)findViewById(R.id.txt_author); 
    mTitleView = (TextView)findViewById(R.id.post_topic); 
    mDateView = (TextView)findViewById(R.id.post_date_time); 
    mCommentsRecycler = (RecyclerView) findViewById(R.id.recycler_comments); 
    mCommentsRecycler.setLayoutManager(new LinearLayoutManager(this)); 
    mCommentField = (EditText) findViewById(R.id.comment_field); 
    Button mCommentButton = (Button) findViewById(R.id.button_post_comment); 
    mCommentButton.setOnClickListener(this); 

    // Get post key from intent 
    String mPostKey = getIntent().getStringExtra(EXTRA_POST_KEY); 
    if (mPostKey == null) { 
     throw new IllegalArgumentException("Must pass EXTRA_POST_KEY"); 
    } 
    // Initialize Database 
    mPostReference = FirebaseDatabase.getInstance().getReference().child("mainboard").child(mPostKey); 
    mCommentsReference = FirebaseDatabase.getInstance().getReference().child("cm-mainboard").child(mPostKey); 


} 

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

    // Add value event listener to the post 
    ValueEventListener postListener = new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      // Get Post object and use the values to update the UI 
      PostMainboard post = dataSnapshot.getValue(PostMainboard.class); 
      User user = dataSnapshot.getValue(User.class); 

      mAuthorView.setText(user.uid); 
      mTitleView.setText(post.postTopic); 
      mBodyView.setText(post.postDetail); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      // Getting Post failed, log a message 
      Log.w(TAG, "loadPost:onCancelled", databaseError.toException()); 
      Toast.makeText(MainboardDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show(); 
     } 
    }; 
    mPostReference.addValueEventListener(postListener); 

    // Keep copy of post listener so we can remove it when app stops 
    mPostListener = postListener; 

    // Listen for comments 
    mAdapter = new CommentAdapter(this, mCommentsReference); 
    mCommentsRecycler.setAdapter(mAdapter); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    if (mPostListener != null) { 
     mPostReference.removeEventListener(mPostListener); 
    } 
    mAdapter.cleanupListener(); 
} 

Post類

public class PostMainboard{ 
public String uid; 
public String auther; 
public String postTopic; 
public String postDetail; 
public String postImageUrl; 
public String postID; 
private String postlatlon; 
public long timeCreated; 

public PostMainboard(){ 

} 

public PostMainboard(String uid, String auther , String postTopic , String postDetail,long timeCreated) 
{ 
    this.uid = uid; 
    this.auther = auther; 
    this.postTopic = postTopic; 
    this.postDetail = postDetail; 
    this.postImageUrl = postImageUrl; 
    this.postID = postID; 
    this.timeCreated = timeCreated; 
} 

@Exclude 
public Map<String, Object> toMap() { 
    HashMap<String, Object> result = new HashMap<>(); 
    result.put("mb_id", uid); 
    result.put("mb_auther" , auther); 
    result.put("mb_title", postTopic); 
    result.put("mb_body", postDetail); 
    result.put("mb_create", timeCreated); 
    return result; 
} 

User類

public class User { 
public String uid; 
public String user_fname; 
private String user_lname; 
private String user_idcard; 
private String email; 
private String user_phone; 

public User(){ 

} 


public User(String uid ,String user_fname, String user_lname, String user_idcard, String email, String user_phone) { 
    this.uid = uid; 
    this.user_fname = user_fname; 
    this.user_lname = user_lname; 
    this.user_idcard = user_idcard; 
    this.email = email; 
    this.user_phone = user_phone; 
} 

ViewHolder

public class MainboardViewHolder extends RecyclerView.ViewHolder { 
private TextView authorView; 
private TextView bodyView; 
private TextView titleView; 
private TextView dateView; 

public MainboardViewHolder(View itemView) { 
    super(itemView); 
    authorView = (TextView)itemView.findViewById(R.id.txt_author); 
    bodyView = (TextView)itemView.findViewById(R.id.post_detail); 
    titleView = (TextView)itemView.findViewById(R.id.post_topic); 
    dateView = (TextView)itemView.findViewById(R.id.post_date_time); 
} 

public void bindToPost (PostMainboard postMainboard) 
{ 
    authorView.setText(postMainboard.auther); 
    bodyView.setText(postMainboard.postDetail); 
    titleView.setText(postMainboard.postTopic); 
    dateView.setText((int) postMainboard.timeCreated); 
} 

主板片段

public class MainboardFragment extends PostListFragment{ 


public MainboardFragment() { 
    // Required empty public constructor 
} 

@Override 
public Query getQuery(DatabaseReference databaseReference) { 
    return databaseReference.child("mainboard").orderByKey(); 
} 

我很希望大家幫助我,我不懂太多

謝謝

回答

0

它看起來像你忘記填充ViewHolder(MainboardViewHolder)。

在「populateViewHolder」下的「PostlistFragment」中,您需要調用viewHolder.bindToPost(model)將數據綁定到文本視圖等等。

試試這個:

@Override 
    protected void populateViewHolder(final MainboardViewHolder viewHolder, final PostMainboard model, final int position) { 
     final DatabaseReference postRef = getRef(position); 

     viewHolder.bindToPost(model); 

     viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(mActivity, MainboardDetailActivity.class); 
       intent.putExtra(MainboardDetailActivity.EXTRA_POST_KEY, postRef.getKey()); 
       startActivity(intent); 
      } 
     }); 
    } 

此外,您getQuery方法是抽象的,空洞的。它大概應該是這樣的:

@Override 
public Query getQuery(DatabaseReference databaseReference) { 
    return databaseReference.getReference("mainboard").orderByKey(); 
} 

我說「可能」,是因爲我看不到你如何初始化mDatabase。將來,如果你想要更好更快的答案,請粘貼完整的代碼。

+0

我認爲不是這個先生我需要顯示數據即時嘗試到您的代碼,但不能 –

+0

我的代碼只是提供了一個上下文,並故意不完整。但是如果你發佈了MainboardViewHolder.java的代碼,我可以給你一個完整的例子。 – henrikenblom

+0

對不起,先生我張貼我的觀點已經plz再次檢查 –