android – How to assign date/time to specific item in RecyclerView?-ThrowExceptions

Exception or error:

I’m developing a simple To do App. The recyclerview gets items from Sqlite datebase.
By clicking the specific item, i want to assign date from Calendar to that item. Now i want to schedule each item in recyclerview with date. I’m facing problem to assign date/time to specific item because when i add date, it shows as next item in recyclerview

 @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {
        final InboxViewModel inboxViewModel = (InboxViewModel) arrayList.get(position);

        holder.textView.setText(inboxViewModel.getText());
        holder.date.setText(inboxViewModel.getDate());
        setAnimation(holder.itemView, position);
MainClass is:
            public class InboxFragment extends Fragment{
        RecyclerView inbox_recyclerview;Inbox_Adapter adapter;
            ArrayList arrayList;
        String myId,myTask,myDate; int pos;String dt;
            MyDatabase db;
            private InboxViewModel inboxViewModel;
            FragmentHelper fragmentHelper; BottomSheetListener bottomSheetListener;
         RelativeLayout relativeLayout; DataController dataController;
         ActivityMain activityMain;

            @Override
            public void onAttach(@NonNull Context context) {
                super.onAttach(context);
                fragmentHelper = (FragmentHelper) context;
                bottomSheetListener=(BottomSheetListener)context;
                dataController=(DataController)context;
                activityMain=(ActivityMain)context;

            }



            public View onCreateView(@NonNull LayoutInflater inflater,
                                     ViewGroup container, Bundle savedInstanceState) {
                View root = inflater.inflate(R.layout.fragment_inbox, container, false);
                relativeLayout=root.findViewById(R.id.relative1);
                arrayList=new ArrayList<>();
                inbox_recyclerview=root.findViewById(R.id.inbox_recycler);
                inbox_recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
                inbox_recyclerview.setHasFixedSize(true);
               // dataController.onGetData();

                MyDatabase myDatabase = new MyDatabase(getContext());
                Cursor c = myDatabase.getData();
                if (c.getCount() == 0) {
                    Toast.makeText(getContext(), "no data available", Toast.LENGTH_SHORT).show();
                } else {
                    if (c.moveToFirst()) {
                        do {
                            myId = c.getString(c.getColumnIndex(myDatabase.id));
                           myTask = c.getString(c.getColumnIndex(myDatabase.task_col));
                         myDate = c.getString(c.getColumnIndex(myDatabase.date_col));
                            inboxViewModel =new InboxViewModel(myTask,myId,myDate);
                            arrayList.add(inboxViewModel);

                        } while (c.moveToNext());
                        {

                            adapter=new Inbox_Adapter(getActivity(),arrayList,fragmentHelper,bottomSheetListener);
                            inbox_recyclerview.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }
                        }
                    }


                    inbox_recyclerview.setItemAnimator(new DefaultItemAnimator());
                    inbox_recyclerview.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));

                return root;
            }


    }
    MainFragmentClass is:
    public class InboxFragment extends Fragment{
    RecyclerView inbox_recyclerview;Inbox_Adapter adapter;
        ArrayList arrayList;
    String myId,myTask,myDate; int pos;String dt;
        MyDatabase db;
        private InboxViewModel inboxViewModel;
        FragmentHelper fragmentHelper; BottomSheetListener bottomSheetListener;
     RelativeLayout relativeLayout; DataController dataController;
     ActivityMain activityMain;

        @Override
        public void onAttach(@NonNull Context context) {
            super.onAttach(context);
            fragmentHelper = (FragmentHelper) context;
            bottomSheetListener=(BottomSheetListener)context;
            dataController=(DataController)context;
            activityMain=(ActivityMain)context;

        }



        public View onCreateView(@NonNull LayoutInflater inflater,
                                 ViewGroup container, Bundle savedInstanceState) {
            View root = inflater.inflate(R.layout.fragment_inbox, container, false);
            relativeLayout=root.findViewById(R.id.relative1);
            arrayList=new ArrayList<>();
            inbox_recyclerview=root.findViewById(R.id.inbox_recycler);
            inbox_recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
            inbox_recyclerview.setHasFixedSize(true);
           // dataController.onGetData();

            MyDatabase myDatabase = new MyDatabase(getContext());
            Cursor c = myDatabase.getData();
            if (c.getCount() == 0) {
                Toast.makeText(getContext(), "no data available", Toast.LENGTH_SHORT).show();
            } else {
                if (c.moveToFirst()) {
                    do {
                        myId = c.getString(c.getColumnIndex(myDatabase.id));
                       myTask = c.getString(c.getColumnIndex(myDatabase.task_col));
                     myDate = c.getString(c.getColumnIndex(myDatabase.date_col));
                        inboxViewModel =new InboxViewModel(myTask,myId,myDate);
                        arrayList.add(inboxViewModel);

                    } while (c.moveToNext());
                    {

                        adapter=new Inbox_Adapter(getActivity(),arrayList,fragmentHelper,bottomSheetListener);
                        inbox_recyclerview.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                    }
                }
How to solve:

Leave a Reply

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