Firebase RecyclerView + ViewHolder。如何保存状态活动

时间:2018-03-21 20:24:32

标签: android firebase android-recyclerview android-viewholder

大家。 你好。我已经创建了android应用程序。我正在从实时数据库中检索数据,还使用循环视图+视图持有者。但当我去另一个活动,然后回去 - 我的名单哦查看持有人项目被重置。我如何保存状态视图持有者并恢复它们? 我查看了网站上的各种主题,但没有一个答案对我没有帮助。 我的Pojo课程

public class Post {
private String  post_id;
private String  post_title;
private long  post_date;
private long  post_desc;
public Post(String post_id, String post_title, long post_date,  String post_desc) {
    this.post_id = post_id;
    this.post_title = post_title;
    this.comments_count = comments_count;
    this.post_date = post_date;
    this.post_desc = post_desc;
}     
public void setPost_id(String post_id) {
    this.post_id = post_id;
}
public String getPost_title() {
    return post_title;
}

public void setPost_title(String post_title) {
    this.post_title = post_title;
}

public String getPost_date() {
    String month = new java.text.SimpleDateFormat("MM").
            format(new java.util.Date(post_date * 1000));
    int monthnumber = Integer.parseInt(month);

    String value = new java.text.SimpleDateFormat("dd ").
            format(new java.util.Date(post_date * 1000));
    value +=MonthName[monthnumber-1];
    value+= new java.text.SimpleDateFormat(" HH:mm").
            format(new java.util.Date(post_date * 1000));
    return value;
}

public void setPost_date(long post_date) {
    this.post_date = post_date;
}

public String getPost_desc() {
    return post_desc;
}

public void setPost_desc(String post_desc) {
    this.post_desc = post_desc;
}}     

ViewHolderClass

public static class PostViewHolder extends  RecyclerView.ViewHolder {
    public PostViewHolder(View itemView) {
        super(itemView); }
   public void setTitle(String title) {
        titleViewPost.setText(title);
    }
    public void setDate(String date) {
        dateViewPost.setText(date);
    }
    public void setDesc(String desc) {
        descViewPost.setText(desc);
    }}}    

FirebaseRecyleAdapter

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder >(
                Post.class,
                R.layout.post_row,
                PostViewHolder.class,
                mDatabase
        ) {@Override    
            protected void populateViewHolder(final PostViewHolder viewHolder,      
Post model, final int position) {     
  viewHolder.mCommentButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent commentActivity  = new Intent(MainActivity.this, CommentActivity.class);
                        commentActivity.putExtra("post_id", post_key);
                        if(mUserId != null) {
                            commentActivity.putExtra("user_id", mUserId);
                        }
                        startActivity(commentActivity);
                    }
                });

据我了解,整个问题是我的适配器被重复使用,我无法返回磁带中的那个位置 - 我之前的位置。

我的应用。https://play.google.com/store/apps/details?id=dev.arounda.chesnock 我想解决这个应用程序的麻烦

1 个答案:

答案 0 :(得分:0)

尝试调用firebaseRecyclerAdapter.startListening();在onCreate()而不是onStart和firebaseRecyclerAdapter.stopListening();在onDestroy()而不是onStop()