young_08
young_08

Reputation: 1216

changing items in recyclerview adapter

I inserted/removing from particular position in ArrayList onBindViewHolder . Now , i want to show this modified list on recyclerview .

Adapter Code:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyAdapterViewHolder> {
    private List<Info> dataList;
    private Context mAct;
    private List<Info> totalCandidatesList;
    private  String TAG = "OWD";

    public MyAdapter(List<Info> dataList, Context context) {
        this.dataList = dataList;
        this.mAct = context;
    }

    public void addApplications(List<Info> candidates) {

        if(this.totalCandidatesList == null){
            totalCandidatesList = new ArrayList<>();
        }
        this.dataList.addAll(candidates);
        this.totalCandidatesList.addAll(candidates);
        this.notifyItemRangeInserted(0, candidates.size() - 1);

    }

    public void clearApplications() {
        int size = this.dataList.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                dataList.remove(0);
                totalCandidatesList.remove(0);
            }
            this.notifyItemRangeRemoved(0, size);
        }
    }

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


    public void onBindViewHolder(MyAdapterViewHolder mAdapterViewHolder, int i) {

        if (i % 2 == 1) {
            mAdapterViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#ecf5fe"));
            mAdapterViewHolder.layoutRipple.setBackgroundColor(Color.parseColor("#ecf5fe"));
        } else {
            mAdapterViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#e2f1ff"));
            mAdapterViewHolder.layoutRipple.setBackgroundColor(Color.parseColor("#e2f1ff"));
        }


        final WorkHolders workHolders = SingleTon.getInstance().getWorkHolders();
        final String customerName = SingleTon.getInstance().getCustomerName();
        String siteName = null;
        if(customerName !=null) {
            String[] sitenamearray = customerName.split("--");

            if (sitenamearray.length > 1) {
                siteName = sitenamearray[1];
            }
        }
        final Info ci = dataList.get(i);
        mAdapterViewHolder.title.setText(ci.heading1);
        mAdapterViewHolder.jobNumber.setText(ci.heading2);
        mAdapterViewHolder.distance.setText(ci.distance);

        if(siteName != null && siteName.equalsIgnoreCase(ci.heading2)) {
            mAdapterViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#a7ffeb"));
            mAdapterViewHolder.layoutRipple.setBackgroundColor(Color.parseColor("#a7ffeb"));
            if(i!=0){


>  //Here i removed and inserted item in list . 
>                     dataList.remove(i);
>                     dataList.add(0,ci);

            }
      }

        final String finalSiteName = siteName;
        final Bundle bundle = new Bundle();

        mAdapterViewHolder.layoutRipple.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment;

                String name = ci.heading1 + "--" + ci.heading2;

                Log.d(TAG,"new Jobname : "+ name);
                if (finalSiteName == null || finalSiteName.equalsIgnoreCase("")) {

                    bundle.putString("name", customerName);
                    bundle.putString("oldwork", "yes");
                    bundle.putString("running_job_selected", "yes");
                } else {

                    Log.d(TAG,"StartedOn Before Sending Bundle :" + workHolders.startedOn);
                    Log.d(TAG, "running Job is not selected");

                    bundle.putString("name", name);
                    bundle.putString("oldwork", "yes");
                    bundle.putString("running_job_selected", "no");
                }

                FragmentTransaction ft = ((FragmentActivity) mAct).getSupportFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.glide_fragment_horizontal_in, R.anim.glide_fragment_horizontal_out);

                fragment = new WorkDescriptionFragment();

                fragment.setArguments(bundle);
                ft.addToBackStack("myadapter");
                ft.replace(R.id.content_frame, fragment).commit();
                SingleTon.getInstance().setWorkStatus("start");
            }
        });

    }

    @Override
    public MyAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.single_item1, viewGroup, false);

        return new MyAdapterViewHolder(itemView);
    }

    public static class MyAdapterViewHolder extends RecyclerView.ViewHolder {

        protected TextView title;
        protected TextView dateTime;
        protected TextView distance;
        protected TextView jobNumber;

        protected CardView cardView;
        protected LayoutRipple layoutRipple;

        public MyAdapterViewHolder(View v) {
            super(v);
            title = (TextView) v.findViewById(R.id.title);
            dateTime = (TextView) v.findViewById(R.id.dateTimeTextView);
            distance = (TextView) v.findViewById(R.id.distanceTextView);
            jobNumber = (TextView) v.findViewById(R.id.jobNumber);
            cardView = (CardView) v.findViewById(R.id.cv);
            layoutRipple = (LayoutRipple)v.findViewById(R.id.singleitemripple);
        }
    }

}

you will see following lines in above code where i am removing/inserting item in a list onBindview and would like to show same in recyclerview .

But right now i am getting normal datalist(unchanged) .

//Here i removed and inserted item in list . 
                      dataList.remove(i);
                      dataList.add(0,ci);

Please help me to achieve this .

Upvotes: 2

Views: 1503

Answers (1)

nutella_eater
nutella_eater

Reputation: 3572

onBindViewHolder is not the place where you should update your adapter. The staregy is to update item inside your Adapter data list and then notifyDataChanged(). For example thise are the methods for updating info inside my adapter:

    public void update(Track track) {
        tracks.remove(track);
        add(track);
    }

    public void add (Track track) {
        tracks.add(track);
        this.notifyDataSetChanged();
    }

    public void addTracks(List<Track> tracks){
        this.tracks.addAll(tracks);
        this.notifyDataSetChanged();
    }

    public void clearAndAddTracks(List<Track> tracks) {

        for (int i = 0; i < this.tracks.size(); i++) {
            if (!this.tracks.get(i).isRunning()){

            }
        }
        this.tracks.clear();
        this.tracks.addAll(tracks);
        this.notifyDataSetChanged();
    }

Upvotes: 2

Related Questions