0

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.

Harish Kamboj
  • 759
  • 10
  • 30
  • To notify a recycler view of a data set change you must use notifyDatasetChanged(); http://stackoverflow.com/questions/24740557/notifydatasetchanged-not-working-on-recyclerview – Jesson Atherton Jul 19 '16 at 15:22
  • I am calling notifyDatasetChanged(); see postadapter.java – Harish Kamboj Jul 19 '16 at 15:23
  • Yeah but it's called in the constructor. this is another post about how to use it properly. http://stackoverflow.com/questions/31367599/how-to-update-recyclerview-adapter-data – Jesson Atherton Jul 19 '16 at 15:29

2 Answers2

0

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()

Ramin
  • 346
  • 3
  • 19
  • @ Ramin, In onBindViewHolder all the values are binded but to I did't show this in my code check my question, I updated the coding part. – Harish Kamboj Jul 19 '16 at 15:35
0

why not use a contentprovider and LoaderCallback. I will automatically do all that for you.

Checkout my example https://github.com/codephillip/CarContentProvider

It might be of help

Phillip Kigenyi
  • 843
  • 9
  • 20
  • My all code is setup and i am near to make to properly working and If I'll use this example than it will take few days to understand + implementation because am new in android ...... – Harish Kamboj Jul 19 '16 at 15:53