5

am trying to update my adapter after adding some data in it but its not updating , when i switch through other activities and come back the adapter got updated this time my code :

    void updateAdapter(){
    adapter.setData(getData());
    adapter.notifyDataSetChanged();
    Log.d("TAG"," DATA SIZE AFTER UPDATE : "+getData().size() +" items : "+adapter.getItemCount());

}

my adapter :

    private void createAdapter(final RecyclerView recyclerView , final List<Information> data) {
    List<Information> content = new ArrayList<>();

    Log.d("TAG","createAdapter Called");

    final int[] currentRowPosition = {0};
    content = data;
    final List<Information> finalContent = content;
     adapter = new ParallaxRecyclerAdapter<Information>(finalContent) {

/// my onBindViewHolder
        @Override
        public void onBindViewHolderImpl(final RecyclerView.ViewHolder viewHolder, final ParallaxRecyclerAdapter<Information> adapter, final int i) {


            currentRowPosition[0] = i;

            if ( i <= finalContent.size() -1 ){

                ((ViewHolder) viewHolder).linearLayout.setVisibility(View.GONE);

            final Information current = finalContent.get(i);

            ((ViewHolder) viewHolder).DESCRIPTION.setText(current.description);

        }else {

                 // this is my loadMore button which stays at the last row of recyclerView 
                ((ViewHolder) viewHolder).setIsRecyclable(false);
                ((ViewHolder) viewHolder).linearLayout.setVisibility(View.VISIBLE);
                ((ViewHolder) viewHolder).itemView.getLayoutParams().height = 100;


                ((ViewHolder) viewHolder).LOADMORE.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        fetchNext();
                    }
                });

            }

        }
//my on create View Holder
        @Override
        public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, final ParallaxRecyclerAdapter<Information> adapter, int i) {

            return  new ViewHolder(getActivity().getLayoutInflater().inflate(R.layout.custom_row, viewGroup, false));

        }

        @Override
        public int getItemCountImpl(ParallaxRecyclerAdapter<Information> adapter) {
            return finalContent.size()+1;
        }
    });
// adding header to recyclerView  
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    View header = getActivity().getLayoutInflater().inflate(R.layout.header, recyclerView, false);

    headerImageView =  (ImageView) header.findViewById(R.id.headerImageView);
    //setting CoverImage

    adapter.setParallaxHeader(header, recyclerView);

    adapter.setData(content);
    recyclerView.setAdapter(adapter);

}

any idea how can i add new items with realtime updation on my RecyclerView ?? any guidance will be so helpful for me

remy boys
  • 2,675
  • 5
  • 28
  • 57
  • How are you adding new data? in separate activity? Could you also add your code for activity that hosts recyclerView – Marat Jul 08 '16 at 08:57
  • yes in same activity , `updateAdapter` is where am adding new data @Marat – remy boys Jul 08 '16 at 09:10
  • Instead of refreshing whole recylceView you can simply refresh a single item see this [How to update/refresh specific item in RecyclerView](http://stackoverflow.com/questions/32457406/how-to-update-refresh-specific-item-in-recyclerview). – Janki Gadhiya Jul 08 '16 at 09:32

3 Answers3

4

I am assuming that when you run the Activity (which has RecyclerView) for the first time it loads everything from the adapter properly.

But when you change to other activities which would change items to your adapter and come back to your Activity which has RecyclerView in it can't get the proper updated information as adapter is not updated.

This may be because you are not refreshing your adapter when your activity resumes in onResume(), something like this. you may have to make other changes to your onResume method as required,

onResume(){
  if(adapter != null){
     adapter.notifyDataSetChanged();
   }
}

Kindly check the lifecycle of an Activity

enter image description here

Example: This does not directly addresses your issue but help you understand as you come back to activity you should refresh the adapter.

There is a NoteListActivity, with ListView in it, you may Add or edit items to the Adapter in it in NoteEditActivity and as soon as you come back you have to refresh the adapter in the activity where you have your listview

Pankaj Nimgade
  • 4,259
  • 3
  • 17
  • 29
1

Adapter.class

public void add(List<Information> informations){
try{
finalContent.addAll(informations);
}catch(Exception e){
e.printstacktrace();
}
notifyDataSetChanged();
}
Narendra Sorathiya
  • 3,426
  • 2
  • 30
  • 35
0

well i ended up with this :

   int offset =   adapter.getItemCount();
    for(int l=0; l<= 4 ; l++){ 

        Log.d("TAG","getData : "+getData().size());
        if ((offset - 2) + l < getData().size()){

            adapter.addItem(getData().get((offset - 2 ) + l) , adapter.getItemCount() - 2);
            adapter.notifyDataSetChanged();

        }
    }

what i needed was adapter.addItem(item , position) and adapter.notifyDataSetChanged(); the above example is loading 5 images at a time

remy boys
  • 2,675
  • 5
  • 28
  • 57