1

everyone. Hello. I’ve created android application. I am retrieving data from real-time database and also using recycle view + view holder. But when I go to another activity and then go back - my list oh view holder items was reset. How I can save state view holder and restore them? I looked at various topics on the site, but not one answer did not help me. My Pojo Class

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);
                    }
                });

As I understand, the whole problem is that my adapter is reused and I can not return to that position in the tape - where I was before.

Its my app.https://play.google.com/store/apps/details?id=dev.arounda.chesnock I want fix thiw trouble for this application

1 Answers1

0

Try to call firebaseRecyclerAdapter.startListening(); in onCreate() instead of onStart and firebaseRecyclerAdapter.stopListening(); in onDestroy() instead of onStop()

Karim Karimov
  • 403
  • 1
  • 6
  • 14