Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to access an ArrayList from a recycleradapter class to a fragment.
@Override
public void onBindViewHolder(final DataObjectHolder holder, final int position)
{
    holder.setIsRecyclable(false);
    holder.itemid.setText(mDataset.get(position).getItem_id());
    holder.itemname.setText(mDataset.get(position).getItem_name());
    holder.qty.setText(mDataset.get(position).getQty());
    int  rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
    holder.rate.setText(formatter.format(rt)+".00");
    ItemDetails listItem = filterList.get(position);
    holder.itemname.setText(listItem.getItem_name());
    holder.plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            holder.qty.setText(String.valueOf(++qt));
            mDataset.get(position).setQty(String.valueOf(qt));
            finalDataset.add(new ItemDetails(ItemFragment.getInstance().cat_id,String.valueOf(holder.itemid.getText()),String.valueOf(holder.itemname.getText()),String.valueOf(holder.rate.getText()),String.valueOf(holder.qty.getText())));
        }
    });
    holder.minus.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(qt==0)
            {
                holder.qty.setText(String.valueOf(0));
            }
            else
            {
                holder.qty.setText(String.valueOf(--qt));
            }
            mDataset.get(position).setQty(String.valueOf(qt));
            finalDataset.add(new ItemDetails(ItemFragment.getInstance().cat_id,String.valueOf(holder.itemid.getText()),String.valueOf(holder.itemname.getText()),String.valueOf(holder.rate.getText()),String.valueOf(holder.qty.getText())));
        }
    });
}

finalDataset is the mentioned ArrayList. I added the values to the ArrayList inside the onBindViewHolder method in recycleradapter class.

What I have tried:

I have tried with for loop to access the arraylist in the fragment,but unfortunately the arraylist is not accessible there.
Posted
Updated 18-May-18 3:58am
v3
Comments
David Crow 8-May-18 8:41am    
"...but unfortunately the arraylist is not accessible there."

Why not? Where is finalDataset declared?

1 solution

If you have defined Arraylist in fragment class, then try by making it public and use it from the adapter class.

It may solve your problem. You can also pass arraylist in the constructor of adapter class.

Here you will send arraylist in parameter from fragment to adapter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900