Recyclerview refreshing items without even calling notifyDataSetChanged() - android

Let me explain you what's happening. I have a fragment and in its onCreateView() I am setting the adapter to recyclerview without any item..
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
downloadsList = new ArrayList<>();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
downloadsRecyclerview.setLayoutManager(linearLayoutManager);
downloadsList.clear();
downloadAdapter = new DownloadAdapter(context, downloadsList);
downloadsRecyclerview.setAdapter(downloadAdapter);
}
Now the weird part is whenever I add items to downloadsList ( which is basically an ArrayList ), then my recyclerview get updated with the content available on downloadsList. I am not even calling notifyDataSetChanged() or setting new adapter again... Any help..

If you don't want to reload entire RecyclerView using notifyDataSetChanged() then simple add notifyItemInserted()
when you add value in downloadsList then after just call
downloadAdapter.notifyItemInserted(downloadsList.size()-1)

Related

Not able to set data in the recycler view

I wanted to set the data(result)into the recycler view, but when i run, the data is not displayed on the screen
RPCRequest.OnResponseListener orderListener = new RPCRequest.OnResponseListener<ArrayList<Person>>() {
#Override
public void onSuccessResponse(RPCRequest request, ArrayList<Person> response) {
ArrayList<Person> dups=new ArrayList<>();
for(int i=0;i<response.size();i++){
if(compare(response.get(i).getDate(),Person.getInstance().getDate()))
dups.add(response.get(i));
}
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.form);
View layout=(View) dialog.findViewById(R.id.row);
layout.setVisibility(View.GONE);
Toolbar toolbar = (Toolbar) dialog.findViewById(R.id.toolbar);
toolbar.setTitle(" Duplicates");
final RecyclerView dupOrders = (RecyclerView) dialog.findViewById(R.id.recycler_view);
dupOrders.setAdapter(adapter);
adapter.setData(dups);
dialog.show();
This is my code where i want to set the data in recycler list view. but when i debug i get the value in data. but its not displayed while executing. Can anyone please help.
You need to set a LayoutManager for the RecyclerView.
Add this line:
dupOrders.setLayoutManager(new LinearLayoutManager(context));
You should probably do this just once, so you might want to make your dupOrders variable into a field of your Activity and initialize it and set the LayoutManager in onCreate().

RecyclerView with FirebaseRecyclerAdapter showing wrong values when scrolling

In my Activity, I use a RecyclerView that uses a FirebaseRecyclerAdapter to draw a list:
mChatDetailsAdapter = new ChatDetailsAdapter(
this,
MessageData.class,
R.layout.layout_chat_item,
ChatDetailsAdapter.MessageViewHolder.class,
query);
mChatListView.setHasFixedSize(true);
LinearLayoutManager lLinearLayoutManager = new LinearLayoutManager(ChatDetailsActivity.this, LinearLayoutManager.VERTICAL, false);
lLinearLayoutManager.setStackFromEnd(true);
lLinearLayoutManager.setReverseLayout(true);
mChatListView.setLayoutManager(lLinearLayoutManager);
mChatListView.addItemDecoration(new VerticalSpaceItemDecoration(UIUtils.dpToPx(ChatDetailsActivity.this, 8)));
mChatListView.setAdapter(mChatDetailsAdapter);
The Adapter takes care about the views:
#Override
protected void populateViewHolder(final MessageViewHolder pViewHolder, final MessageData pMessageData, int pPosition) {
pViewHolder.mMessageTextView.setText(pMessageData.getMessage());
pViewHolder.mSelfLinearLayout.setVisibility(View.VISIBLE);
pViewHolder.mTimeTExtView.setText(StringUtils.toMessageTimeString(mContext, pMessageData.getTime()));
}
In the onBindViewHolder() method I basically do the same, be assigning the data from the model (works correctly after looking at the log messages). Now when scrolling the list the RecyclerView changes its values and doesn't recycle correctly and I have no idea why. Any idea?

Setting different adapter in single RecyclerView in Android

I have MenuActivity which has RecyclerView and ViewPager. This ViewPager has 3 pages which use three different Fragment.
FragmentOne,FragmentTwo, FragmentThree, All these Fragments use RecyclerView data.
I want to use three different ArrayList need to be set in RecyclerView Adapter, which is depends on what Fragment user is viewing.
One solution is to put RecyclerView in all three fragment and get update data in Fragment. I just want to know if it is possible to set Adapter data in MenuActivity based on what Fragment is called.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
itemList = new ArrayList<>();
prepareMenuData();
vegadapter = new MenuItemAdapter(getContext(),vegItemList);
nonvegadapter = new MenuItemAdapter(getContext(),nonvegItemList);
dessertAdapter = new MenuItemAdapter(getContext(),dessertItemList);
recyclerView.setAdapter(vegadapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
return rootView;
}
prepareMenuData();
for (MenuItem item:itemList)
{
if(item.getCategory().equals("VEG"))
{
vegItemList.add(item);
vegadapter.notifyDataSetChanged();
}
if(item.getCategory().equals("NONVEG"))
{
nonvegItemList.add(item);
nonvegadapter.notifyDataSetChanged();
}
if(item.getCategory().equals("DESSERTVEG"))
{
dessertItemList.add(item);
dessertAdapter.notifyDataSetChanged();
}
}
This code does not know when to set vegadapter,nonvegadapter etc.
Please suggest.
Thanks
Deepak
Technically yes you can, but whether thats good practice is another question.
You'll have to have the RecyclerView in each Fragment regardless but you can get the data for the adapter from the Activity. Fragments have access to their Activity, thats how the Android lifecycle works so from the Fragment you could call ((MenuActivity) getActivity()).getMenuList(); and then pass the result to that Fragments adapter. This will however couple the Fragment with the Activity which isn't good practice.
Given the fact that it looks like your data isn't dynamic what I would personally do is make your MenuItem class implement Parcelable, then when you create your Fragments you can pass through your ArrayList of MenuItems as an argument, this way your Fragment is independent of your Activity.

Recyclerview inside fragment inside viewpager notifydatasetchanged not working

I have a recyclerview inside a fragment and that fragment is inside a view pager.
notifydatasetchanged() is not working even though the same code works in other projects of mine. The only difference I can see is that it is inside a view pager now?.
This is how notify the change:
public void updateRecyler(){
// Clear the current array
alarmsFromSP.clear();
// Update the array (This is working the array size is changing)
alarmsFromSP.addAll(AlarmCollection.getAlarms(getActivity()));
// Notify the change
adapter.notifyDataSetChanged();
}
I create the recylerview in oncreate like this:
public class FragmentAllAlarms extends Fragment {
// Arraylist of the alarm data from the strings
ArrayList<Alarm> alarmsFromSP;
tabRefreshReceiver r;
RecyclerView rv;
AlarmsAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_all_alarms, container, false);
// Recycler
rv = (RecyclerView) v.findViewById(R.id.mainAlarmRecyclerView);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
// Get saved data for alarms from the shared preferences string
alarmsFromSP = AlarmCollection.getAlarms(getActivity());
System.out.println("StringIs"+alarmsFromSP.size());
// Adapter
adapter = new AlarmsAdapter(getActivity(), alarmsFromSP);
rv.setAdapter(adapter);
Is it possible I am losing reference to the array or is it somthing to do with the viewpager?
I have been stuck with this for two days now any help is greatly appreciated.
Thanks in advance
make sure that updateRecyler() is called,
and try to change adapter.notifyDataSetChanged(); to rv.getAdapter().notifyDataSetChanged();
this worked for me once.
I have faced the same problem, then I found a temporary solution, instead of using notifyDataSetChanged() use
recyclerView.setAdapter(adapter);
recyclerView.scrollToPosition(postion);

How to get reference to a View inside CardView from an activity?

I have got CardView with Views inside a RecyclerView. I have created adapter in which assets are attached to Views and everything works. Now, I would like to change these Views from my activity. Is it any simple way to do that?
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private List<Offer>offers;
TextView timer; //timer inside CardView
private void getViewReferences() {
recyclerView = (RecyclerView)findViewById(R.id.mainRecyclerView);
timer = (TextView)findViewById(R.id.timer);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getViewReferences();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
initializeData();
initializeAdapter();
timer.setText("13:16"); //NullPointerException here
}
private void initializeData(){
offers = new ArrayList<>();
offers.add(new Offer("godzina", R.drawable.zdj, 200));
offers.add(new Offer("godzina", R.drawable.zdj));
}
private void initializeAdapter() {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(offers);
recyclerView.setAdapter(adapter);
}
}
So you don't want to change the Views but you want to update the text values of the views in your list.
You can't directly do that by trying to find the Views in the recyclerView and change the text. The adapter is responsible for giving values to your list. So you need to update the dataset in the adapter and call one of the notifyDataSetChanged methods on the adapter to update the recyclerView.
The big advantage of this design is that it places Views in Cache in order to reuse them multiple times without having to inflate and construct them again. This is a huge performance improvement.
You need to read the docs and some tutorials to understand the adapter design pattern better.

Categories

Resources