简体   繁体   中英

How to pass data from fragment to recyclerview adapter class in android

I am creating an application. In this application I have a recyclerview. My single row of recyclerview has some buttons. I need to launch an actvity(or perform weservice call) when user clicks on that button. I need to pass some data from the fragment which holds the recycler view to the new activity via this adapter. The button click happens in the adapter. I am stuck in this problem and finding it very difficult to solve this, can any one please help me how can I solve this issue. All suggestions are welcome.

My Fragment Code:

/**
 * A simple {@link Fragment} subclass.
 */
public class Toadline extends Fragment implements ToadlineAdapter.ClickListener{




    public Toadline() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_toadline, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewTimeline);



        context = getContext();
        postHeader = getArguments().getStringArrayList("PostHeader");

ToadlineAdapter adapter = new ToadlineAdapter(context, getData1());
            recyclerView.setAdapter(adapter);
            adapter.setClickListener(this);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


            });



        return view;
    }



        @Override
        public void itemClicked (View view,int position){






        }

    public static List<TimelineDataStore> getData1() {

        List<TimelineDataStore> data = new ArrayList<>();

        int[] icons = {R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image};
        Bitmap[] images = profileImageAfterDownload;
        ArrayList<String> titles = postHeader;

for (int i = 0; i < titles.size() && i < icons.length && i < noOfDays.size() && i < postTitle.size(); i++) {

            TimelineDataStore current = new TimelineDataStore();
            current.images = images[i];
            //current.iconId = icons[i];
            current.title = titles.get(i);


            data.add(current);
        }
        return data;
    }
}

My Adapter Code:

    import android.content.Context;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.widget.CardView;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;

    import java.util.Collections;
    import java.util.List;

    import toadways.ways.toad.toadways.R;

    /**
     * Created by Toadways Admin on 19-11-2015.
     */
    public class ToadlineAdapter extends RecyclerView.Adapter<ToadlineAdapter.MyViewHolder> {

        private ClickListener clickListener;
        private SwipeRefreshLayout.OnRefreshListener clickListener1;
        private LayoutInflater inflater;
        List<TimelineDataStore> data = Collections.EMPTY_LIST;

        public ToadlineAdapter(Context context, List<TimelineDataStore> data) {
            inflater = LayoutInflater.from(context);
            this.data = data;


        }


        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = inflater.inflate(R.layout.single_card_row, parent, false);
            MyViewHolder viewHolder = new MyViewHolder(view);
            return viewHolder;
        }


        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            TimelineDataStore current = data.get(position);
            holder.title.setText(current.title);
            holder.images.setImageBitmap(current.images);

          }

        @Override
        public int getItemCount() {

            return data.size();
        }

        public void setClickListener(ClickListener clickListener) {
            this.clickListener = clickListener;
        }

        public void setClickListener1(SwipeRefreshLayout.OnRefreshListener clickListener1) {
            this.clickListener1 = clickListener1;
        }




        // View Holder object for Recycler View
        class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

            TextView title, noOfDays, postTitle, agreeCount, disAgreeCount, neutralCount, conversationCount, postDescription;
            ImageView icon, postImage, images;
            CardView mCardView;
            Button mShowMore, mShowLess;


            public MyViewHolder(View itemView) {
                super(itemView);
                itemView.setOnClickListener((View.OnClickListener) this);
                title = (TextView) itemView.findViewById(R.id.textViewTitle);
                images = (ImageView) itemView.findViewById(R.id.imageViewUser);
                noOfDays = (TextView) itemView.findViewById(R.id.textViewNoOfDays);
                postTitle = (TextView) itemView.findViewById(R.id.textViewPostDescription);
                postImage = (ImageView) itemView.findViewById(R.id.postImage);
                agreeCount = (TextView) itemView.findViewById(R.id.textViewAgreeCount);
                disAgreeCount = (TextView) itemView.findViewById(R.id.textViewDisAgreeCount);
                neutralCount = (TextView) itemView.findViewById(R.id.textViewNeutralCount);
                conversationCount = (TextView) itemView.findViewById(R.id.textViewConversationCount);
                postDescription = (TextView) itemView.findViewById(R.id.textViewPostContentDescription);

                mCardView = (CardView) itemView.findViewById(R.id.card_view);
                mShowMore = (Button) itemView.findViewById(R.id.buttonSeeMore);
                mShowLess = (Button) itemView.findViewById(R.id.buttonSeeLess);

            }

 mShowLess.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                      mContext.startActivity(new Intent(mContext, newActivity.class));

                }
            });

            @Override
            public void onClick(View view) {

                if (clickListener != null) {
                    clickListener.itemClicked(view, getPosition());
                }

            }
        }


        public interface ClickListener {

            public void itemClicked(View view, int position);
        }
    }

I need to pass an array list from the fragment to a new activity from the mShowLess.setOnClickListener method present in adapter. Can anyone please let me know how can I do that or is there a better way to do this.

I'm a beginner, but I think this helps:

If you want to pass and ArrayList to newActivty try use:

@Override
 public void onClick(View v) {
      Intent intent = new Intent(mContext, newActivity.class);
    intent.putExtra("arrayList", yourArrayList);
    mContext.startActivity(intent);
                    }
            });

In newActivity:

protected void onCreate(Bundle savedInstanceState) {
        ...
        ArrayList<String> arrayList = getIntent().getExtras().getStringArrayList("arrayList");
    }

In your Fragment set data to the adapter

recyclerView.setAdapter(new ToadlineAdapter( context,getData1()));

In your Adapter inside the constructor assign data

  private Context context;
  private ArrayList<ProductRequestObject> data;
  public ToadlineAdapter(Context context, List<TimelineDataStore> data) {
            data = data;
            this.context = context;
   } 

Then override the onclick method in your adapter

        @Override
        public void onClick(View v) {

        // Here You will get data of the selected position  
            Bundle bumdle;
            bumdle = new Bundle();
            bumdle.putSerializable("selectedPosition ", data.get(getAdapterPosition()));


        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM