android – How to pass data adapter to fragment class using interface?-ThrowExceptions

Exception or error:

I am trying to pass data Adapter class to Fragment using Interface. I have created prize_breakup.setOnClickListener When I click on button then Fragment will load and get data from Adapter. When I run code It’s not showing any error but it’s not showing result also.

……………………………………This is adapter Class……………………………………

public class LiveContestAdapter extends RecyclerView.Adapter<LiveContestAdapter.MyViewHolder> {
    Context context;
    List<Contest_Data> contest_data;
    public TextView Quantity;
    public ImageButton btnIncrease,btnDecrease;
    private int mCounter = 1;
    private LiveContest  live_contest;
    Bundle bundle = new Bundle();

    public interface LiveContest{
        void onClick(int position);
    }

 public LiveContestAdapter(Context context,  List<Contest_Data> contest_data, LiveContest live_contest) {
        this.context=context;
        this.contest_data=contest_data;
        this.live_contest = live_contest;
    }
public LiveContestAdapter(Context context,  List<Contest_Data> contest_data) {
    this.context=context;
    this.contest_data=contest_data;
}

    @NonNull
    @Override
   public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
  context=viewGroup.getContext();
View vh=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_list_win_iphone,viewGroup,false);
        MyViewHolder myViewHolder=new MyViewHolder(vh);

        return myViewHolder;
    }



@Override
 public void onBindViewHolder(@NonNull final LiveContestAdapter.MyViewHolder myViewHolder, final int i) {
            final Contest_Data contest_position = contest_data.get(i);
            Picasso.with(context).load(contest_data.get(i).getContestImage()).error(R.drawable.damme).placeholder(R.drawable.damme).into(myViewHolder.img);
            myViewHolder.remainSlot.setText(contest_data.get(i).getContestTotal_slot());
            myViewHolder.ent_fee.setText(contest_data.get(i).getContestEntry_Fee());
            myViewHolder.Booked_Slot.setText(contest_data.get(i).getContestBooked());
            String contest_status = contest_data.get(i).getContestStatus();
            String previous_contest = contest_data.get(i).getPrevious_contest();
            String winner_announce = contest_data.get(i).getWinner_announce();


     myViewHolder.prize_breakup.setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View v)
                {
                    live_contest.onClick(i);
                }
            });    
        }

        @Override
        public int getItemCount() {
            return contest_data.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

            TextView name;
            ImageView img;
            TextView remainSlot;
            Button ent_fee;
            TextView totalslot;
            Button tandc;
            TextView eDate;
            Button share;
            Button prize_breakup;
            TextView Booked_Slot;
            ImageButton btnplus;
            ImageButton btnminus;
            TextView counter;
            Button htp;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                //name=itemView.findViewById(R.id.totalslot);
                img=itemView.findViewById(R.id.imageView2);
                htp = itemView.findViewById(R.id.button2);
                remainSlot=itemView.findViewById(R.id.tvTotalSlotNumber);
                ent_fee= itemView.findViewById(R.id.btnPrice);
                Booked_Slot = itemView.findViewById(R.id.tvSlotBookNumber);
                eDate = itemView.findViewById(R.id.tvDate1);
                tandc = itemView.findViewById(R.id.btnTerms);
                share= itemView.findViewById(R.id.btnShare);
                prize_breakup = itemView.findViewById(R.id.button3);
                btnplus = itemView.findViewById(R.id.ibIncrease);
                btnminus = itemView.findViewById(R.id.ibDecrese);
                counter  = itemView.findViewById(R.id.tvQuantity);
                //entery_fee = itemView.findViewById(R.id.buy_ticket);
            }
        }
    }

………………………This Fragment Class……………………………………..

public class PrizeLayout extends Fragment implements LiveContestAdapter.LiveContest {

    List<Contest_Data> contest_data ;
    LiveContestAdapter live_contest_adapter;
    TextView nameFragTxt,yearFragTxt;
    String contest_id, contest_name;
    Intent intent;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container , Bundle savedInstanceState)
    {

        View rootView =inflater.inflate(R.layout.prize_layout,container,false);


        live_contest_adapter = new LiveContestAdapter(getContext(), contest_data, new LiveContestAdapter.LiveContest(){
        @Override
        public void onClick(int position) {
        Toast.makeText(getContext(), "click ok button at" + position, LENGTH_LONG).show();
    }
        });


        return rootView;
    }


    @Override
    public void onClick(int position) {

    }
}
How to solve:

Try using this

Fragment :-

 public class HomeFragment extends Fragment {

    List<String> contest_data;
    LiveContestAdapter live_contest_adapter;

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.home_fragment, container, false);

        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        live_contest_adapter = new LiveContestAdapter(getContext(), contest_data, new LiveContestAdapter.LiveContest() {
            @Override
            public void onClick(int position) {
                Toast.makeText(getContext(), "click ok button at" + position, LENGTH_LONG).show();
            }
        });
        recyclerView.setAdapter(live_contest_adapter);

        return view;
    }


}

Adapter :-

    public class LiveContestAdapter extends RecyclerView.Adapter<LiveContestAdapter.MyViewHolder> {
    Context context;
    List<String> contest_data;
    public TextView Quantity;
    public ImageButton btnIncrease, btnDecrease;
    private int mCounter = 1;
    private LiveContest live_contest;
    Bundle bundle = new Bundle();

    public interface LiveContest {
        void onClick(int position);
    }

    public LiveContestAdapter(Context context, List<String> contest_data, LiveContest live_contest) {
        this.context = context;
        this.contest_data = contest_data;
        this.live_contest = live_contest;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        context = viewGroup.getContext();
        View vh = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_list_win_iphone, viewGroup, false);
        MyViewHolder myViewHolder = new MyViewHolder(vh);

        return myViewHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull final LiveContestAdapter.MyViewHolder myViewHolder, final int pos) {
        myViewHolder.prize_breakup.setText("Button " + pos);

        myViewHolder.prize_breakup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                live_contest.onClick(pos);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 10;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        Button prize_breakup;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            prize_breakup = itemView.findViewById(R.id.prize_breakup);
        }
    }
}

Output is

click here

Leave a Reply

Your email address will not be published. Required fields are marked *