Multiple recyclerviews using one adapter - java

I have an app where I want to use recyclerviews in different parts of the application and I was thinking of using the same adapter for the same two (or more) recyclerviews, I was thinking this would be a good practice, or am I mistaken and better off making a new adapter for every recyclerview in the app?
Here is the code for my adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerViewAdapter.MyViewHolder> {
private ArrayList<Task> list;
//This is the list of the second recyclerview which I want to implement
private ArrayList<String> categories;
private OnItemListener mOnItemListener;
RecyclerViewAdapter(ArrayList<Task> list, OnItemListener onItemListener)
{
this.list = list;
this.mOnItemListener = onItemListener;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
TextView textView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view, parent, false);
return new MyViewHolder(textView, mOnItemListener);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.textView.setText(list.get(position).getTaskName());
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView textView;
OnItemListener onItemListener;
MyViewHolder(#NonNull View itemView, OnItemListener onItemListener) {
super(itemView);
textView = itemView.findViewById(R.id.singleView);
this.onItemListener = onItemListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
onItemListener.onItemClick(getAdapterPosition());
}
}
public interface OnItemListener
{
void onItemClick(int position);
}
}
So how would I go about doing this so that it can switch depending on which recyclerview in the app is setting/initializing the adapter, so that the getItemCount for example, would return the size of categories instead of list and so on.

Related

Android Studio: Deleting Item from ChildRecyclerView

I'm currently building a schedule app where users can input information, and the app will organize the information in a list with each day of the week as the header with each corresponding activity under the day. I'm using a parent child recyclerview to do this (i.e. each day of the week is a part of the parent recyclerview and each activity in the schedule is part of the child recyclerview). Right now I'm trying to add a swipe to delete information in the child recyclerview. Does anyone know how I would go about doing this using the parent child recyclerview configuration, where I have two different adapters for each recyclerview?
Code for Parent RecyclerView:
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.ViewHolder> {
ArrayList<DayHeader> weekList;
private Context mContext;
public MainRecyclerViewAdapter(Context mContext, ArrayList<DayHeader> weekList) {
this.mContext = mContext;
this.weekList = weekList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.dayofweekheader,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
DayHeader section = weekList.get(position);
String nameofday = section.getDayName();
ArrayList<medinfo> meditems = section.getmMedItems();
holder.weekdayname.setText(nameofday);
MedicationAdapter childrecyclerAdapter = new MedicationAdapter(mContext,meditems);
holder.childrecyclerview.setAdapter(childrecyclerAdapter);
}
#Override
public int getItemCount() {
return weekList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView weekdayname;
RecyclerView childrecyclerview;
public ViewHolder(#NonNull View itemView) {
super(itemView);
weekdayname = itemView.findViewById(R.id.weekdayheader);
childrecyclerview = itemView.findViewById(R.id.childrecyclerview);
}
}
}
Code for Child RecyclerView:
public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.MedicationViewHolder>{
private ArrayList<medinfo> mMedList;
private Context mContext;
public static class MedicationViewHolder extends RecyclerView.ViewHolder {
public TextView mMedname;
public TextView mTime;
public TextView mNumbPills;
public ImageView popup;
public MedicationViewHolder(#NonNull View itemView) {
super(itemView);
mMedname = itemView.findViewById(R.id.medname);
mTime = itemView.findViewById(R.id.time);
mNumbPills = itemView.findViewById(R.id.numberofpills);
popup = itemView.findViewById(R.id.menumore);
}
}
public MedicationAdapter(Context mContext, ArrayList<medinfo> medList) {
this.mContext = mContext;
this.mMedList = medList;
}
#NonNull
#Override
public MedicationViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,parent,false);
MedicationViewHolder mvh = new MedicationViewHolder(v);
return mvh;
}
#Override
public void onBindViewHolder(#NonNull MedicationViewHolder holder, int position) {
medinfo currentItem = mMedList.get(position);
String medName = "Medication: " + currentItem.getmMedName();
holder.mMedname.setText(medName);
String medTime = "Time: " + currentItem.getmTime();
holder.mTime.setText(medTime);
String numbPills = "# of Pills: " + currentItem.getmNumbPills();
holder.mNumbPills.setText(numbPills);
}
#Override
public int getItemCount() {
return mMedList.size();
}
public void removeItem(int position) {
mMedList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mMedList.size());
}
}
I also have this SwipeItem class:
public class SwipeItem extends ItemTouchHelper.SimpleCallback {
MedicationAdapter mMedAdapter;
SwipeItem(MedicationAdapter mMedAdapter){
super(0,ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT);
this.mMedAdapter = mMedAdapter;
}
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getBindingAdapterPosition();
this.mMedAdapter.removeItem(position);
}
}
So how can I implement the SwipeItem in my adapter codes to delete each child item?

How Can I store RecylerView With Use SQliteOpenHelper

I have recyclerview on my project . and It can work successfuly . I want store my recyclerview with use SQliteOpenHelper but ıf you know another basic storing way then write please I can another storeing way. Can you be fast It's important .
MainActivity.java
public class MainActivity extends AppCompatActivity implements BottomSheetDialogx.BottomSheetListener {
Button listeo;
RecyclerView recyclerView;
List<String> Lists = new ArrayList<>();
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todolist);
recyclerView=findViewById(R.id.recyclerviewx);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapter=new CustomAdapter(Lists);
recyclerView.setAdapter(adapter);
}
#Override
public void onButtonClick(String text) {
Lists.add(text);
adapter.notifyItemInserted(Lists.size()-1);
}
}
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.Holder> {
List<String> Lists;
public CustomAdapter(List<String>itemxx){
this.Lists = itemxx;
}
#NonNull
#Override
public CustomAdapter.Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_item,parent,false);
Holder holder = new Holder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull CustomAdapter.Holder holder, int position) {
holder.textView.setText(Lists.get(position));
}
#Override
public int getItemCount() {
return Lists.size();
}
public class Holder extends RecyclerView.ViewHolder {
TextView textView;
public Holder(View view) {
super(view);
textView=view.findViewById(R.id.text_view);
}
}
}
Use Room library instead of SQliteOpenHelper.

Getting onSwipe event from a nested RecyclerView to the parent Fragment

I'm developping an app in which my main activity hosts different fragments via a SectionsPageAdapter. My question is about one of those fragments that roughtly looks like the image bellow:
My innerRecyclers lists differents items that I want to be able to remove by swiping and make the user validate the deletion via a DialogFragment. I was able to implement the swipe action but what I'm struggly to do is to implement a listener in the OuterRecycler for OnItemTouch so I can use an interface and send the action back to my fragment which will dispay my DialoFragment afterwards.
The code of my OuterRecycler currently looks like that:
public class OuterRecyclerAdapter extends RecyclerView.Adapter<OuterRecyclerAdapter.InsideRecyclerHolder> {
private final Context mContext;
private final String mText;
private final ArrayList<MainViewArrayListMasterList> mDataArrayList;
OuterRecyclerAdapter(Context context, ArrayList<MainViewArrayListMasterList> DataArrayList, String text) {
mContext = context;
mDataArrayList = RecyclerArrayListType;
mText = text;
}
#NonNull
#Override
public OuterRecyclerAdapter.InsideRecyclerHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater myInflater = LayoutInflater.from(mContext);
View myOwnView = myInflater.inflate(R.layout.outer_recycler, parent, false);
return new OuterRecyclerAdapter.InsideRecyclerHolder(myOwnView);
}
#Override
public void onBindViewHolder(#NonNull OuterRecyclerAdapter.InsideRecyclerHolder holder, int position) {
MainViewArrayListMasterList currentItem = mDataArrayList.get(position);
holder.CardTitle.setText(String.valueOf(currentItem.getmYear()));
LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
holder.CardRecycler.setLayoutManager(mLayoutManager);
InnerRecyclerAdapter historyInnerView_Adapter = new InnerRecyclerAdapter(mContext, currentItem.getmArrayListStored(), mText);
new ItemTouchHelper(ListHistoryViewSwiped).attachToRecyclerView(holder.CardRecycler);
holder.CardRecycler.setAdapter(historyInnerView_Adapter);
}
#Override
public int getItemCount() {
return mDataArrayList.size();
}
static class InsideRecyclerHolder extends RecyclerView.ViewHolder {
final TextView CardTitle;
final RecyclerView CardRecycler;
InsideRecyclerHolder(#NonNull View itemView) {
super(itemView);
CardTitle = itemView.findViewById(R.id.TitleText);
CardRecycler = itemView.findViewById(R.id.Inside_Recycler);
}
}
ItemTouchHelper.SimpleCallback ListHistoryViewSwiped = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
};
}
Is OnItemTouchListener the good way to acheive this ? Or do I have to create a new callback method ?
I actually circumvented this issue by calling the DialogFragment from my OuterRecycler:
public class OuterRecyclerAdapter extends RecyclerView.Adapter<OuterRecyclerAdapter.InsideRecyclerHolder> {
private final Context mContext;
private final String mText;
private final ArrayList<MainViewArrayListMasterList> mDataArrayList;
private FragmentManager mFragmentManager;
OuterRecyclerAdapter(Context context, FragmentManager FragmentManager, ArrayList<MainViewArrayListMasterList> DataArrayList, String text) {
mContext = context;
mFragmentManager = FragmentManager;
mDataArrayList = RecyclerArrayListType;
mText = text;
}
#NonNull
#Override
public OuterRecyclerAdapter.InsideRecyclerHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater myInflater = LayoutInflater.from(mContext);
View myOwnView = myInflater.inflate(R.layout.outer_recycler, parent, false);
return new OuterRecyclerAdapter.InsideRecyclerHolder(myOwnView);
}
#Override
public void onBindViewHolder(#NonNull OuterRecyclerAdapter.InsideRecyclerHolder holder, int position) {
MainViewArrayListMasterList currentItem = mDataArrayList.get(position);
holder.CardTitle.setText(String.valueOf(currentItem.getmYear()));
LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
holder.CardRecycler.setLayoutManager(mLayoutManager);
InnerRecyclerAdapter historyInnerView_Adapter = new InnerRecyclerAdapter(mContext, currentItem.getmArrayListStored(), mText);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
openMyDialog(currentItem.getmArrayListStored().get(viewHolder.getAdapterPosition()));
}
}).attachToRecyclerView(holder.CardRecycler);
holder.CardRecycler.setAdapter(historyInnerView_Adapter);
}
#Override
public int getItemCount() {
return mDataArrayList.size();
}
static class InsideRecyclerHolder extends RecyclerView.ViewHolder {
final TextView CardTitle;
final RecyclerView CardRecycler;
InsideRecyclerHolder(#NonNull View itemView) {
super(itemView);
CardTitle = itemView.findViewById(R.id.TitleText);
CardRecycler = itemView.findViewById(R.id.Inside_Recycler);
}
}
private void openMyDialog(MainViewArrayListRecycler itemToDelete) {
MyDialog myDialog = MyDialog.newInstance(itemToDelete);
myDialog.show(mFragmentManager, "MyDialogDialog");
}
}

What might be the problem with my adapter i seem not to find it``

The view seems not to bind to the ArrayList(ie private ArrayList mEvents;) that i passed to the class ,on the onBindViewHolder(ie holder.mTextView2.setText(current.getcity());) i can seem to access the textView variables(ie public TextView mTextview2;), that i declared in the view holder class. I have tried to make the variables as a member variable but it seems not to work well
public class eventsAdapter extends RecyclerView.Adapter {
private ArrayList<recycleview_items> mEvents;
public eventsAdapter(ArrayList<recycleview_items> passedArray){
//constructor for the adapter calls for which i created
mEvents = passedArray;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//we pass the layout of our card to the adapter here
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_listitem, parent, false);
eventViewHolder events = new eventViewHolder(v);
return events;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
//we pass data from the array list we created to the views
recycleview_items current = mEvents.get(position);
holder.mImageView.setImageResource(current.getImageResource());
holder.mTextView1.setText(current.getname());
holder.mTextView2.setText(current.getcity());
}
#Override
public int getItemCount() {
return mEvents.size();
};
public static class eventViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageview ;
public TextView mTextview;
public TextView mTextview2;
public eventViewHolder(#NonNull View itemView) {
super(itemView);
mImageview = itemView.findViewById(R.id.ImageView1);
mTextview = itemView.findViewById(R.id.text_view1);
mTextview2 = itemView.findViewById(R.id.text_view2);
}
}
}
public class eventsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<recycleview_items> mEvents;
public eventsAdapter(ArrayList<recycleview_items> passedArray){
//constructor for the adapter calls for which i created
mEvents = passedArray;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//we pass the layout of our card to the adapter here
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_listitem, parent, false);
eventViewHolder events = new eventViewHolder(v);
return events;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
//we pass data from the array list we created to the views
recycleview_items current = mEvents.get(position);
holder.bind(current);
}
#Override
public int getItemCount() {
return mEvents.size();
};
class eventViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageview ;
public TextView mTextview;
public TextView mTextview2;
public eventViewHolder(#NonNull View itemView) {
super(itemView);
mImageview = itemView.findViewById(R.id.ImageView1);
mTextview = itemView.findViewById(R.id.text_view1);
mTextview2 = itemView.findViewById(R.id.text_view2);
public void bind(recycleview_items item){
//bind your items here
holder.mImageView.setImageResource(item.getImageResource());
holder.mTextView1.setText(item.getname());
holder.mTextView2.setText(item.getcity());
}
}
}
I've rewritten your code for you with slight modifications.
You should create adapter like this
extends RecyclerView.Adapter<eventsAdapter.eventViewHolder >
Also you should edit
public eventViewHolder onCreateViewHolder
And
onBindViewHolder(eventViewHolder holder, int position)
Try this
public class eventsAdapter extends RecyclerView.Adapter<eventsAdapter.eventViewHolder>{
private ArrayList<recycleview_items> mEvents;
public eventsAdapter(ArrayList<recycleview_items> passedArray){
//constructor for the adapter calls for which i created
mEvents = passedArray;
}
#NonNull
#Override
public eventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_listitem, parent, false);
return new eventViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull eventViewHolder holder, int position) {
recycleview_items current = mEvents.get(position);
holder.mImageview.setImageResource(current.getImageResource());
holder.mTextview.setText(current.getname());
holder.mTextview2.setText(current.getcity());
}
#Override
public int getItemCount() {
return 0;
}
class eventViewHolder extends RecyclerView.ViewHolder {
ImageView mImageview;
TextView mTextview;
TextView mTextview2;
public eventViewHolder(View itemView) {
super(itemView);
mImageview = itemView.findViewById(R.id.ImageView1);
mTextview = itemView.findViewById(R.id.text_view1);
mTextview2 = itemView.findViewById(R.id.text_view2);
}
}
}
Almost every thing was in Your code that's i have done some modification.
Mate see your Holder View Class name its 'eventViewHolder' and for onBindViewHolder and onCreateViewHolder you're accessing the Recycler.ViewHolder so it's very obvious that you can't access those variable just change this..
#Override
public void onBindViewHolder(#NonNull eventsAdapter.eventViewHolder holder, int position) {
.....
}
and in
#NonNull
#Override
public eventsAdapter.eventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
......
}

Using multiple views and data sets in RecyclerView

I have the following adapter which populates a RecyclerView with CardView items containing one TextView. It fills these TextViews with data from a String array. However, now I want to add another TextView to my CardViews and populate it with a separate data array, and I was wondering how I can best achieve this:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.grade);
}
}
public RecyclerAdapter(String[] myDataset) {
mDataset = myDataset;
}
#Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
All help is appreciated as always!
Simply add another TextView or any other view in your CardLayout XML R.layout.card_item
Then
public TextView mTextView;
public TextView mTextView1;
public TextView mTextView2;
public TextView mTextView3;
public ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.grade);
mTextView1 = (TextView)itemView.findViewById(R.id.grade1);
mTextView2 = (TextView)itemView.findViewById(R.id.grade2);
mTextView3 = (TextView)itemView.findViewById(R.id.grade3);
}
and finally set data to all the desired textviews as
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText("String");
holder.mTextView1.setText("String");
holder.mTextView2.setText("String");
holder.mTextView3.setText("String");
}

Categories

Resources