35227

accessing an adapter's data via another adapter

Question:

Is it all possible to access an adapter's data via another adapter? I want to start an activity and pass data from an adapter to a fragment which is used in TabLayout as one of three fragments, I have two adapters and a button which is clicked to start an activity, its Java code is in my first adapter and I need to pass second adapter's data via second adapter itself here is my codes:

my first adapter:

public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder>{ private ArrayList<SectionDataModel> dataList; private Context mContext; private RecyclerView.RecycledViewPool recycledViewPool; private SnapHelper snapHelper; public RecyclerViewDataAdapter(ArrayList<SectionDataModel> dataList, Context mContext) { this.dataList = dataList; this.mContext = mContext; recycledViewPool = new RecyclerView.RecycledViewPool(); } @Override public ItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null); ItemRowHolder rowHolder = new ItemRowHolder(v); snapHelper = new GravitySnapHelper(Gravity.START); return rowHolder; } @Override public void onBindViewHolder(ItemRowHolder holder, int position) { ArrayList singleSectionItems = dataList.get(position).getAllItemInSection(); final String sectionName = dataList.get(position).getHeaderTitle(); holder.itemTitle.setText(sectionName); SectionDataAdapter adapter = new SectionDataAdapter(singleSectionItems, mContext); holder.recyclerView.setHasFixedSize(true); holder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)); holder.recyclerView.setAdapter(adapter); holder.recyclerView.setRecycledViewPool(recycledViewPool); snapHelper.attachToRecyclerView(holder.recyclerView); holder.btnMore.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //here i can start the activity but..(second adapter) Toast.makeText(view.getContext(), sectionName, Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return (null != dataList ? dataList.size() : 0); } public class ItemRowHolder extends RecyclerView.ViewHolder { protected ImageView mitemImage; protected TextView mitemtext; protected TextView itemTitle; protected RecyclerView recyclerView; protected Button btnMore; public ItemRowHolder(View itemView) { super(itemView); this.mitemImage = itemView.findViewById(R.id.itemImage); this.mitemtext = itemView.findViewById(R.id.tvTitle); this.itemTitle = itemView.findViewById(R.id.itemTitle); this.recyclerView = itemView.findViewById(R.id.recycler_view_list); this.btnMore = itemView.findViewById(R.id.btnMore); } } } '

and my second adapter:

import java.net.PortUnreachableException; import java.util.ArrayList; public class SectionDataAdapter extends RecyclerView.Adapter<SectionDataAdapter.SssingleItemRowHolder>{ private ArrayList<SingleItemModel> itemModels; private Context mContext; public SectionDataAdapter(ArrayList<SingleItemModel> itemModels, Context mContext) { this.itemModels = itemModels; this.mContext = mContext; } @Override public SingleItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_single_card, null); SingleItemRowHolder singleItemRowHolder = new SingleItemRowHolder(v); return singleItemRowHolder; } @Override public void onBindViewHolder(SingleItemRowHolder holder, int position) { SingleItemModel itemModel = itemModels.get(position); holder.tvTitle.setText(itemModel.getName()); holder.mitemImage.setImageResource(itemModel.getImage()); } @Override public int getItemCount() { return (null != itemModels ? itemModels.size() : 0); } public class SingleItemRowHolder extends RecyclerView.ViewHolder { protected TextView tvTitle; protected ImageView mitemImage; public SingleItemRowHolder(View itemView) { super(itemView); final Intent intent = new Intent(mContext,MainActivity.class); this.mitemImage = itemView.findViewById(R.id.itemImage); this.tvTitle = itemView.findViewById(R.id.tvTitle); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //... need to start the activity from here Toast.makeText(view.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show(); } }); } }

Answer1:

Like <strong>@MilaDroid</strong> said you simply need a getter that returns the another Adapter's ArrayList<SingleItemModel> but the problem you will face is that you need to have the same instance of the Adapter from the Activity in order to get the populated ArrayList<SingleItemModel>.

<blockquote>

A good workaround is to used Bill Pugh's Singleton in the Adapter

</blockquote> public class Adapter { private ArrayList<SingleItemModel> list; private Adapter() {} public static Adapter getInstance() { return InstInit.INSTANCE; } // Don't forget to set the list (or NPE) // because we can't argue with a Singleton public void setList(ArrayList<SingleItemModel> list) { this.list = list; } // You can now get the ArrayList public ArrayList<SingleItemModel> getList() { return list; } private static class InstInit { private static final Adapter INSTANCE = new Adapter(); } // Overrided RecyclerView.Adapter Methods ................. } <blockquote>

Retrieving the ArrayList assuming that the following Adapters are Singleton

</blockquote> AdapterOne a1 = AdapterOne.getInstance(); AdapterTwo a2 = AdapterTwo.getInstance(); ArrayList<SingleItemModel> a1RetrievedList = a1.getList(); // You don't need to create a new instance // creating a new instance doesn't make sense // because you need to repopulate the list // for the new instance. ArrayList<SingleItemModel> a2RetrievedList = a2.getList(); // You can also retrieve from AdapterTwo

Recommend

  • Grails GORM sort by CASE statement
  • Filter array of tuples in swift
  • iPhone SDK: Grouped UITableView Sections, List Items not showing under correct section [closed]
  • After first click, If ajax is done, on second click e.stopPropagation
  • Android: removing and adding views to a recyclerView properly
  • data doesn't show up on recyclerview - got error No layout manager attached; skipping layout
  • How to use ExpandableRecyclerView library with multiple (child) viewTypes?
  • E/RecyclerView: No adapter attached; skipping layout (Using Fragment)
  • Load only x items in Firebase Recycler Adapter
  • Put characters received from an array in alphabetical order without using sorting functions?
  • Android: ListAdapter example redraws same content
  • VBA, TRIM part of a Path
  • Picasso Taking time to load images
  • Galaxy S4 Android Custom Camera Issue
  • Regarding starting the threads on a condition
  • Linq Merge lists
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • GridView breaks while scrolling
  • Blackberry - Custom EditField Cursor
  • how to adjust image in a panel in Java swing?
  • Body moving without any force applied? (Box2d)
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • JTable with a ScrollPane misbehaving
  • Android Google Maps API OnLocationChanged only called once
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?