Search code examples
androidandroid-recyclerviewnotifydatasetchanged

Data is not inserting dynamically in RecyclerView from sqlite


Hello guys am trying to load data dynamically in Recyclerview here is my code :

PostAdapter.java

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {

ArrayList<Post> listData;
String rpostid, ruserid, rname, rcat, rdate, rtittle;

public PostAdapter(ArrayList<Post> postList) {
    this.listData = postList;
    notifyDataSetChanged();
}

public PostAdapter(ArrayList<Post> postList,int i) {
    this.listData = postList;
    this.notifyItemRangeInserted(getItemCount()+1,i);
}

@Override
public PostAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.post_item, viewGroup, false);
    return new posts(v);
}

public class posts extends ViewHolder {
    TextView tvTittle, tvPostId, tvUseId, tvName, tvCat, tvDate;

    public posts(View v) {
        super(v);
        /* View stuf */
    }
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    final posts holder = (posts) viewHolder;
    Post post = listData.get(position);
    rpostid = post.getPost_id();
    ruserid = post.getUser_id();
    rname = post.getUser_name();
    rcat = post.getPost_cat();
    rdate = post.getPost_time();
    rtittle = post.getPost_title();
    holder.tvPostId.setText(rpostid);
    holder.tvUseId.setText(ruserid);
    holder.tvName.setText(rname);
    holder.tvCat.setText(rcat);
    holder.tvDate.setText(rdate);
    holder.tvTittle.setText(rtittle);
}

@Override
public int getItemCount() {
    return listData.size();
}

}

Here is my code for updating data when swipeToRefresh is call

public void newPost(int s){
    postNew = handler.getNewPost(s);//getting data from DB
    postList.addAll(postNew);
    adapter = new PostAdapter(postNew);   
    listView.setAdapter(adapter);
}

Here is the code for adding data at the bottom while scrolling

public void oldPost(int s){
    postNew = handler.getOldPost(s);// getting data from db
    postList.addAll(postNew);
    adapter = new PostAdapter(postNew,postNew.size());
    listView.setAdapter(adapter);
}

I want to make data insertion dynamically on the top of recyclerView when SwipeToRefresh is called and want to show old data at the bottom of recyclerview when scroll down. New data is inserted but not dynamically instead recyclerview refreshes and goes to its beginning.

All the thing is working fine like getting the bottom of recycleview and calling the oldPost() + Swipe to refresh to call newPost(). The problem is only that data is not loading dynamically please help me in this. Thanks in advance.


Solution

  • UPDATE after the edit:

    you need to move your update code out of the constructor and to a normal method, the constructor "constructs" a new object instance and does not magically update anything:

    public void InsertInPoistion(ArrayList<Post> postList,int i) {
        int begin = listData.size();
        this.listData.addAll(postList);
        int end = listData.size();
        this.notifyItemRangeInserted(begin ,end);
    }
    

    then call this method inside your fragment/activity:

    mAdapter.InsertInPoistion(postNew,postNew.size());
    

    mAdapter here is a reference you keep after instantiating your adapter for the first time. only create a new adapter in onCreate()