I am having trouble displaying the fragment nothing showed up in my activity. My goal is to save the sharedpreference as gson since arraylist cannot be saved into sharedpreference and then use gson to display the arraylist in recyclerview in fragments
this is from my activity where it saves the user input data into json
private void saveDataToSharedPreference(String categoryId, String categoryName, Integer eventCount, Boolean switchActivated){
ArrayList<CategoryList> category_list = new ArrayList<CategoryList>();
SharedPreferences preferences = getSharedPreferences("CategoryInfo",MODE_PRIVATE);
SharedPreferences.Editor edit = getPreferences(MODE_PRIVATE).edit();
category_list.add(new CategoryList(categoryId,categoryName,eventCount,switchActivated));
String json = gson.toJson(category_list);
edit.putString("category_info",json);
edit.apply();
}
this is my fragment to display the recyclerview
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
myRecyclerView = view.findViewById(R.id.categoryRecyclerView);
myRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
layoutManager = new LinearLayoutManager(getView().getContext());
myRecyclerView.setLayoutManager(layoutManager);
MyRecyclerAdapter myRecyclerAdapter = new MyRecyclerAdapter(getContext(),listCategory);
myRecyclerView.setAdapter(myRecyclerAdapter);
myRecyclerAdapter.notifyDataSetChanged();
}
this is my recycler adapter
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomViewHolder>{
Context context;
ArrayList<CategoryList> categoryArrayList = new ArrayList<CategoryList>();
public MyRecyclerAdapter(Context context,ArrayList<CategoryList> categoryArrayList){
this.context = context;
this.categoryArrayList = categoryArrayList;
}
@NonNull
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_card_layout, parent, false);
return new CustomViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
holder.tvCategoryId.setText(categoryArrayList.get(position).getCat_id());
holder.tvCategoryName.setText(categoryArrayList.get(position).getCat_name());
holder.tvEventCount.setText(String.valueOf(categoryArrayList.get(position).getEvent_count()));
if (categoryArrayList.get(position).getIs_active()){
holder.tvIsActive.setText("Yes");
} else {
holder.tvIsActive.setText("No");
}
}
@Override
public int getItemCount() {
return categoryArrayList.size();
}
public static class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView tvCategoryId;
public TextView tvCategoryName;
public TextView tvEventCount;
public TextView tvIsActive;
public CustomViewHolder(@NonNull View itemView) {
super(itemView);
tvCategoryId = itemView.findViewById(R.id.tv_id);
tvCategoryName = itemView.findViewById(R.id.tv_name);
tvEventCount = itemView.findViewById(R.id.tv_eventcount);
tvIsActive = itemView.findViewById(R.id.tv_active);
}
}
}
I logged the json file and its saving properly as it shows:
[{“cat_id”:”CFO-6328″,”cat_name”:”hello”,”event_count”:231,”is_active”:false}]
at logcat so I am confused about what should I do get the fragments to show in my activity
Inu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.