0

hello everyone I have a fragment and I can add all of the elements into the recyclerview with no problem. Then if I delete from inside the recyclerview everything is fine. but then after I delete something it seems that the item that I deleted from the adapters list isn't deleted from the fragments list that I passed into the adapter because if I add something else all of the deleted items go back into the recyclerview. I have logged the size of the list and it is correct its just that if I delete an item or multiple items when I add 1 element to the recycler it adds all of the other deleted ones but it seems that it only counts as one because if I click delete on any of the old ones they all disappear again, I have been at this for a while and I cannot find the solution, thank you for your time

here is the relevant code from the fragment

 public void instantiateViews(){
        View power1 = (View)getView().findViewById(R.id.include);
        View mega1 = (View)getView().findViewById(R.id.include1);
        powerBall = (TextView)power1.findViewById(R.id.win);
        megaMillions = (TextView)mega1.findViewById(R.id.winm);
        power = (RecyclerView)getView().findViewById(R.id.powerRecycle);
        mega = (RecyclerView)getView().findViewById(R.id.megaRecycle);
    }
    public void getMyTickets(){
        powerNumbers = SharedPrefHelper.getMyTickets(getActivity(),"powerball");
        megaNumbers = SharedPrefHelper.getMyTickets(getActivity(),"megamillions");
    }
    public void setMyTickets(){
        SharedPrefHelper.setMyTickets(getActivity(),powerNumbers,"powerball");
        SharedPrefHelper.setMyTickets(getActivity(),megaNumbers,"megamillions");
    }
    public void populateRecyclerViews(){
        //power.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        power.setLayoutManager(llm);
        powerAdapter = new MyTicketAdapter(powerNumbers,"powerball");
        power.setAdapter(powerAdapter);

        //mega.setHasFixedSize(true);
        LinearLayoutManager llm1 = new LinearLayoutManager(getActivity());
        mega.setLayoutManager(llm1);
        megaAdapter = new MyTicketAdapter(megaNumbers,"megamillions");
        mega.setAdapter(megaAdapter);
    }
    public void setClickListeners(){
        powerBall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
// ...Irrelevant code for customizing the buttons and title
                LayoutInflater inflater = getActivity().getLayoutInflater();
                final View dialogView = inflater.inflate(R.layout.ticket_selector, null);
                final NumberPicker one = (NumberPicker)dialogView.findViewById(R.id.one);
                one.setMinValue(1);
                one.setMaxValue(69);
                final NumberPicker two = (NumberPicker)dialogView.findViewById(R.id.two);
                two.setMinValue(1);
                two.setMaxValue(69);
                final NumberPicker three = (NumberPicker)dialogView.findViewById(R.id.three);
                three.setMinValue(1);
                three.setMaxValue(69);
                final NumberPicker four = (NumberPicker)dialogView.findViewById(R.id.four);
                four.setMinValue(1);
                four.setMaxValue(69);
                final NumberPicker five = (NumberPicker)dialogView.findViewById(R.id.five);
                five.setMinValue(1);
                five.setMaxValue(69);
                final NumberPicker six = (NumberPicker)dialogView.findViewById(R.id.six);
                six.setMinValue(1);
                six.setMaxValue(26);
                final RadioGroup group = (RadioGroup)dialogView.findViewById(R.id.group);

                dialogBuilder.setView(dialogView);




                final AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", null, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Add", null, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //powerNumbers = SharedPrefHelper.getMyTickets(getActivity(),"powerball");
                        //powerNumbers = powerAdapter.mDataset;
                        String ticket = one.getValue() + " " + two.getValue() + " " + three.getValue() +
                                " " + four.getValue() + " " + five.getValue() + " " + six.getValue();
                        int mul = group.getCheckedRadioButtonId();
                        RadioButton but = dialogView.findViewById(mul);
                        String multi = but.getText().toString();
                        MyTicket ticket1 = new MyTicket();
                        ticket1.ticket = ticket;
                        ticket1.multi = multi;
                        powerNumbers.add(ticket1);
                        Log.d("adapter",powerNumbers.size()+"");
                        powerAdapter.notifyItemInserted(powerNumbers.size() - 1);
                        SharedPrefHelper.setMyTickets(getActivity(),powerNumbers,"powerball");
                        alertDialog.dismiss();

                    }
                });
                alertDialog.show();
            }
        });
        megaMillions.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
// ...Irrelevant code for customizing the buttons and title
                LayoutInflater inflater = getActivity().getLayoutInflater();
                final View dialogView = inflater.inflate(R.layout.ticket_selector, null);
                final NumberPicker one = (NumberPicker)dialogView.findViewById(R.id.one);
                one.setMinValue(1);
                one.setMaxValue(69);
                final NumberPicker two = (NumberPicker)dialogView.findViewById(R.id.two);
                two.setMinValue(1);
                two.setMaxValue(69);
                final NumberPicker three = (NumberPicker)dialogView.findViewById(R.id.three);
                three.setMinValue(1);
                three.setMaxValue(69);
                final NumberPicker four = (NumberPicker)dialogView.findViewById(R.id.four);
                four.setMinValue(1);
                four.setMaxValue(69);
                final NumberPicker five = (NumberPicker)dialogView.findViewById(R.id.five);
                five.setMinValue(1);
                five.setMaxValue(69);
                final NumberPicker six = (NumberPicker)dialogView.findViewById(R.id.six);
                six.setMinValue(1);
                six.setMaxValue(26);
                six.setBackgroundColor(Color.YELLOW);
                final RadioGroup group = (RadioGroup)dialogView.findViewById(R.id.group);

                dialogBuilder.setView(dialogView);




                final AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", null, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Add", null, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //megaNumbers.clear();
                        //megaNumbers = SharedPrefHelper.getMyTickets(getActivity(),"megamillions");
                       // megaNumbers = new ArrayList<MyTicket>(megaAdapter.mDataset);
                        String ticket = one.getValue() + " " + two.getValue() + " " + three.getValue() +
                                " " + four.getValue() + " " + five.getValue() + " " + six.getValue();
                        int mul = group.getCheckedRadioButtonId();
                        RadioButton but = dialogView.findViewById(mul);
                        String multi = but.getText().toString();
                        MyTicket ticket1 = new MyTicket();
                        ticket1.ticket = ticket;
                        ticket1.multi = multi;
                        megaNumbers.add(ticket1);
                        megaAdapter.notifyItemInserted(megaNumbers.size() - 1);
                        //megaAdapter.notifyDataSetChanged();
                        SharedPrefHelper.setMyTickets(getActivity(),megaNumbers,"megamillions");
                        alertDialog.dismiss();

                    }
                });
                alertDialog.show();
            }
        });
    }

and here is my whole recyclerview adapter

public class MyTicketAdapter extends RecyclerView.Adapter<MyTicketAdapter.MyViewHolder> {
    public ArrayList<MyTicket> mDataset;
    private String type;


    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        CardView cv;
        LinearLayout linear;
        Context context;
        public MyViewHolder(View itemView, Context context) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cv);
            linear = (LinearLayout) itemView.findViewById(R.id.linear);
            this.context = context;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyTicketAdapter(ArrayList<MyTicket> myDataset, String type) {
        mDataset = myDataset;
        this.type = type;

    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyTicketAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
        MyViewHolder vh = new MyViewHolder(v,parent.getContext());
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if(type.equals("powerball")){
            final View view;
            LayoutInflater inflater = (LayoutInflater)   holder.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.balls, null);
            TextView ball1 = (TextView) view.findViewById(R.id.ball1);
            TextView ball2 = (TextView) view.findViewById(R.id.ball2);
            TextView ball3 = (TextView) view.findViewById(R.id.ball3);
            TextView ball4 = (TextView) view.findViewById(R.id.ball4);
            TextView ball5 = (TextView) view.findViewById(R.id.ball5);
            TextView ball6 = (TextView) view.findViewById(R.id.ball6);
            TextView multi = (TextView) view.findViewById(R.id.multi);
            multi.setText("Multiplier: " + mDataset.get(position).multi);
            TextView win = (TextView)view.findViewById(R.id.win);
            win.setText(holder.context.getResources().getString(R.string.remove));

            TextView[] images = {ball1,ball2,ball3,ball4,ball5,ball6};
            //String num = mDataset.get(position).ticket;
            String num = mDataset.get(position).ticket;
            String[] split = num.split(" ");
            for(int j = 0;j < split.length;j++){
                images[j].setText(split[j]);
            }
            holder.linear.addView(view);
            win.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //holder.linear.removeView(view);
                    //mDataset.remove(holder.getAdapterPosition());
                    mDataset.remove(holder.getAdapterPosition());
                    Log.d("adapter",mDataset.size()+"");
                    notifyItemRemoved(holder.getAdapterPosition());
                    notifyItemRangeChanged(holder.getAdapterPosition(), getItemCount());
                    //notifyDataSetChanged();

                    SharedPrefHelper.setMyTickets(holder.context,mDataset,"powerball");
                }
            });
        }else{
            View view;
            LayoutInflater inflater = (LayoutInflater)   holder.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.balls1, null);
            TextView ball1 = (TextView) view.findViewById(R.id.ball1m);
            TextView ball2 = (TextView) view.findViewById(R.id.ball2m);
            TextView ball3 = (TextView) view.findViewById(R.id.ball3m);
            TextView ball4 = (TextView) view.findViewById(R.id.ball4m);
            TextView ball5 = (TextView) view.findViewById(R.id.ball5m);
            TextView ball6 = (TextView) view.findViewById(R.id.ball6m);
            TextView multi = (TextView) view.findViewById(R.id.multim);
            multi.setText("Multiplier: " + mDataset.get(position).multi);
            TextView win = (TextView)view.findViewById(R.id.winm);
            win.setText(holder.context.getResources().getString(R.string.remove));
            TextView[] images = {ball1,ball2,ball3,ball4,ball5,ball6};
            String num = mDataset.get(position).ticket;
            String[] split = num.split(" ");
            for(int j = 0;j < split.length;j++){
                images[j].setText(split[j]);
            }
            holder.linear.addView(view);
            win.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //holder.linear.removeView(view);
                    mDataset.remove(holder.getAdapterPosition());

                    notifyItemRemoved(holder.getAdapterPosition());
                    notifyItemRangeChanged(holder.getAdapterPosition(), 1);
                    //notifyDataSetChanged();

                    SharedPrefHelper.setMyTickets(holder.context,mDataset,"megamillions");
                }
            });
        }

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }
JRowan
  • 6,520
  • 6
  • 34
  • 57

1 Answers1

1

Maybe need remove view from recycler

recycler.removeViewAt(position);

looks this

But i think if you try change list in out of adaper on get callbacks invokation from adapter , maybe you logic becamy more crleary .

I mean on Kotlin:

 interface RemoveListern{
      fun onRemove()
    }

     class Adapter(val listenr:RemoveListener)...


 on Activity:

          val adapter = Adapter(object:RemoveListener{

           oveeride fun onRemove(){
                //<----  MODIFY HERE
                list.remove(position);
                recycler.removeViewAt(position);
                mAdapter.notifyItemRemoved(position);                 
                mAdapter.notifyItemRangeChanged(position, list.size())
           }
})
Serg Burlaka
  • 1,867
  • 19
  • 31
  • how would I call that from inside the adapter though, I am inflating views inside the adapter and the button for deleting is inside the inflated view? – JRowan Dec 08 '18 at 03:09
  • via some callbackListener, but not sure if it will solve you issue. Neet try to detect what wrong – Serg Burlaka Dec 08 '18 at 03:13
  • @jRowan as for me better practic to use diffUtils for this case and change list in out of adaper on get callbackse invokation from adapter – Serg Burlaka Dec 08 '18 at 03:16
  • @jRowan i think if you try change list in out of adaper on get callbacks invokation from adapter , maybe you logic becamy more crleary – Serg Burlaka Dec 08 '18 at 03:17
  • i did this but i had to do list.remove(position); recycler.removeAllViews(); mAdapter.notifyDataSetChanged(); for it to work – JRowan Dec 08 '18 at 04:08