Reputation: 805
I am trying to display some items on a recycleView inside a fragment. The items come from another fragment. I first create the recycleView with a empty arrayList but then when a button is pressed, data is sent through interface and a method populates the ArrayList. I have debugged this the whole day and is working perfect, when I hit the button it logs the size of the ArrayList has gone up to one.
I was reading that I have to notify my adapter when the list changes, so I tried using notifyDataSetChange(), notifyItemInserted(dataReceived.size() -1) but neither seem to work out. whenever I check adapter.getItemCount() it throws me 0, even though the arraylist says throws 1.
here's my code inside the fragment Receiving the data:
public class FavoriteFragment extends Fragment {
private OnFragmentInteractionListener mListener;
RecyclerView myRecyclerView;
public ArrayList<RecycleViewItem> dataReceived;
public MyFavoriteAdapter adapter;
public FavoriteFragment() {
}
public void setDataReceived(ArrayList<RecycleViewItem> dataReceived){
this.dataReceived = dataReceived;
Log.d("INTERFACE", "Data received by the FavoriteFragment!!!!" + " " + this.dataReceived.get(0).getCardName());
for(int i = 0; i < dataReceived.size(); i++) {
//THIS LINE RETURNS A 1
Log.d("INTERFACE", "Items inside the dataReceived: " + dataReceived.size());
//NEITHER WORKED
//adapter.notifyItemInserted(dataReceived.size() -1);
//adapter.notifyDataSetChanged();
//THIS LINE WILL KEEP ON RETURNING 0
Log.d("INTERFACE", "Items inside the list inside the adapter: " + adapter.getItemCount());
}
}
public static FavoriteFragment newInstance() {
FavoriteFragment fragment = new FavoriteFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dataReceived = new ArrayList<>();
adapter = new MyFavoriteAdapter(dataReceived);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_favorite, container, false);
myRecyclerView = (RecyclerView) view.findViewById(R.id.recycleView);
myRecyclerView.setHasFixedSize(true);
LinearLayoutManager myLayoutManager = new LinearLayoutManager(getActivity());
myLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
if (myRecyclerView != null) {
myRecyclerView.setAdapter(adapter);
}
myRecyclerView.setLayoutManager(myLayoutManager);
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
this is the adapter:
public class MyFavoriteAdapter extends RecyclerView.Adapter<MyViewHolder>{
private ArrayList<RecycleViewItem> list = new ArrayList<RecycleViewItem>();
InterfaceListItemClickListener sender = null;
public MyFavoriteAdapter(ArrayList<RecycleViewItem> list) {
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item_favorite, parent, false);
MyViewHolder holder = new MyViewHolder(view, sender);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if(list.size() >= 1) {
holder.favoriteImageView.setImageResource(list.get(position).getImageResourceID());
holder.favoriteTextView.setText(list.get(position).getCardName());
}
}
@Override
public int getItemCount() {
return list.size();
}
}
Upvotes: 0
Views: 78
Reputation: 54194
Your adapter has a private variable list
, and this must be modified for adapter.notifyDataSetChanged()
to have any effect.
Here is your method:
public void setDataReceived(ArrayList<RecycleViewItem> dataReceived){
this.dataReceived = dataReceived;
...
}
Assigning the parameter dataReceived
to your fragment's this.dataReceived
doesn't affect your adapter's list
in any way. Rather, you could do something like this instead:
public void setDataReceived(ArrayList<RecycleViewItem> dataReceived){
adapter.list.addAll(dataReceived);
...
}
You may have to make your adapter's list
variable public, or expose it via a getter:
public ArrayList<RecycleViewItem> getList() {
return list
}
Or even add a method that can add items to it:
public void addAll(ArrayList<RecycleViewItem> items) {
list.addAll(items);
}
Upvotes: 2
Reputation: 1111
public void setDataReceived(ArrayList dataReceived){ this.dataReceived.addAll(dataReceived); .... } Do not recreate list
Upvotes: 0