无法从 Fragment recyclerview 的弹出菜单更新 sqlite 数据库

如何解决无法从 Fragment recyclerview 的弹出菜单更新 sqlite 数据库

在我的应用程序中,我有三个选项卡(课程列表、下载和历史记录),在我的 sqlite 数据库中,我有两列名为 LECTURE_HISTORY_FLAGLECTURE_DOWNLOAD_FLAG认值为零 从 Course List 开始,如果用户想要下载或单击任何项​​目,我可以更新它们。我从嵌入在 RecyclerView 标签片段中的弹出菜单更新它们。我可以很容易地将 LECTURE_HISTORY_FLAGLECTURE_DOWNLOAD_FLAG 从零更新为我想要从我的 Course List 选项卡和 History 选项卡中成功获得的任何值,并查询显示删除它们。我的问题是在一个片段(历史选项卡)中,我可以更新我的数据库,但在另一个片段(下载选项卡)中,我无法更新我的数据库。我在下载选项卡中使用与历史选项卡相同的代码。这是我的代码...

下载Fragment.java 标签

   @Override
    public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.activity_download_list_fragment,container,false);
        download_layout = view.findViewById(R.id.download_layout);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view,Bundle savedInstanceState){
        super.onViewCreated(view,savedInstanceState);
        sampleData();

    }

    public void sampleData(){
        vRecyclerView = Objects.requireNonNull(getView()).findViewById(R.id.download_RecyclerView_fragment);
        try {
            CourseActivityDatabase hCourseDB;
            hCourseDB = new CourseActivityDatabase(context);

            db = hCourseDB.getReadableDatabase();
            vdCourseList = new ArrayList<>();

            cursor = db.query(
                    DB_TABLES.COURSE_TABLE_NAME,new String[]{DB_TABLES.COURSE_ID,DB_TABLES.LECTURE_IMAGE_URL,DB_TABLES.LECTURE_TOPIC,DB_TABLES.LECTURE_GENERAL_INFO,DB_TABLES.LECTURE_DOWNLOAD_FLAG},DB_TABLES.LECTURE_DOWNLOAD_FLAG + " > 0",null,null);


            if (cursor != null && cursor.getCount() != 0) {
                vdCourseList.clear();
                while (cursor.movetoNext()) {
                    SingleItemmodel courseInfo = new SingleItemmodel();

                    int lecture_id = cursor.getInt(cursor.getColumnIndex(DB_TABLES.COURSE_ID));
                    int lectureImg = cursor.getInt(cursor.getColumnIndex(DB_TABLES.LECTURE_IMAGE_URL));
                    String lecturetopic = cursor.getString(cursor.getColumnIndex(DB_TABLES.LECTURE_TOPIC));
                    String lectureInfo = cursor.getString(cursor.getColumnIndex(DB_TABLES.LECTURE_GENERAL_INFO));

                    courseInfo.setFlagId(lecture_id);
                    courseInfo.setLectureImg(lectureImg);
                    courseInfo.setLecturetopic(lecturetopic);
                    courseInfo.setLectureInfo(lectureInfo);

                    vdCourseList.add(courseInfo);

                    if (vdCourseList.size() != 0){
                        download_layout.setVisibility(View.INVISIBLE);
                    }else {
                        download_layout.setVisibility(View.VISIBLE);
                    }
                }
            }
        }catch (sqliteException e){
            Toast.makeText(context,"Database is not available..!!!",Toast.LENGTH_LONG).show();
        }

        vAdapter = new HistoryRecyclerView(context,vdCourseList);
        vRecyclerView.setLayoutManager(new GridLayoutManager(context,1));
        vRecyclerView.setAdapter(vAdapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        cursor.close();
        db.close();
    }

我的 DownloadRecyclerView.java 嵌入了 DownloadFragment.java 片段选项卡..

    @NonNull
    @Override
    public DownloadRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.download_recylerview_activity,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DownloadRecyclerView.ViewHolder holder,int position) {
        final Intent downIntent = new Intent(downContext,PlayActivity.class);


        SingleItemmodel downMain = downOneRowList.get(position);
        holder.download_lectureImg.setimageResource(downMain.getLectureImg());
        holder.download_lecturetopic.setText(downMain.getLecturetopic());
        holder.download_lectureInfo.setText(downMain.getLectureInfo());


        holder.download_vMenu.setonClickListener(v -> {
            PopupMenu popup = new PopupMenu(downContext,holder.download_vMenu);
            popup.inflate(R.menu.download_menu_options);
            popup.setonMenuItemClickListener(item -> {
                switch (item.getItemId()){
                    case (R.id.download_watch):
                        Toast.makeText(downContext,"starting playActivity...",Toast.LENGTH_LONG).show();
                        break;

                    case (R.id.download_remove):

                        //put all the codes for item "add to list" here

                        CourseActivityDatabase downloadDB = new CourseActivityDatabase(downContext);
                        sqliteDatabase db = downloadDB.getWritableDatabase();
                        ContentValues downloadValues = new ContentValues();
                        downloadValues.put(DB_TABLES.LECTURE_HISTORY_FLAG,0);
                        db.update(DB_TABLES.COURSE_TABLE_NAME,downloadValues,DB_TABLES.COURSE_ID + " = ?",new String[]{Integer.toString(downMain.getFlagId())});


                        Toast.makeText(downContext,"Video has been removed from history list!" + " " + position,Toast.LENGTH_SHORT).show();
                        db.close();
                        downOneRowList.remove(position);
                        notifyDataSetChanged();
                        break;
                    default:
                        break;
                }
                return true;
            });
            popup.show();
        });
        holder.download_card.setonClickListener(v ->{
            downIntent.putExtra("downloadItem",position);
            downContext.startActivity(downIntent);
        });
    }

HistoryFragment.java 片段..

 @Override
    public View onCreateView(@NonNull LayoutInflater inflater,Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.activity_history_list_fragment,false);
        his_layout = view.findViewById(R.id.history_layout);
        return view;
    }
    
    @Override
    public void onViewCreated(@NonNull View view,savedInstanceState);
        sampleData();
        vAdapter.notifyItemChanged(vhCourseList.size());

    }

    public void sampleData(){
        vRecyclerView = Objects.requireNonNull(getView()).findViewById(R.id.history_RecyclerView_fragment);

        try {
            CourseActivityDatabase hCourseDB;
            hCourseDB = new CourseActivityDatabase(context);

            db = hCourseDB.getReadableDatabase();
            vhCourseList = new ArrayList<>();

            cursor = db.query(
                    DB_TABLES.COURSE_TABLE_NAME,DB_TABLES.LECTURE_HISTORY_FLAG},DB_TABLES.LECTURE_HISTORY_FLAG + " > 0",null);


            if (cursor != null && cursor.getCount() != 0) {
                vhCourseList.clear();
                while (cursor.movetoNext()) {
                    SingleItemmodel courseInfo = new SingleItemmodel();

                    int lecture_id = cursor.getInt(cursor.getColumnIndex(DB_TABLES.COURSE_ID));
                    int lectureImg = cursor.getInt(cursor.getColumnIndex(DB_TABLES.LECTURE_IMAGE_URL));
                    String lecturetopic = cursor.getString(cursor.getColumnIndex(DB_TABLES.LECTURE_TOPIC));
                    String lectureInfo = cursor.getString(cursor.getColumnIndex(DB_TABLES.LECTURE_GENERAL_INFO));



                    courseInfo.setFlagId(lecture_id);
                    courseInfo.setLectureImg(lectureImg);
                    courseInfo.setLecturetopic(lecturetopic);
                    courseInfo.setLectureInfo(lectureInfo);

                    vhCourseList.add(courseInfo);

                    if (vhCourseList.size() != 0){
                        his_layout.setVisibility(View.INVISIBLE);
                    }else {
                        his_layout.setVisibility(View.VISIBLE);
                    }
                }
            }
        }catch (sqliteException e){
            Toast.makeText(context,vhCourseList);
        vRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),1));
        vRecyclerView.setAdapter(vAdapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        cursor.close();
        db.close();
    }

HistoryRecyclerView.java 嵌入 HistoryFragment.java..

 @NonNull
    @Override
    public HistoryRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_recylerview_activity,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HistoryRecyclerView.ViewHolder holder,int position) {
        final Intent hisIntent = new Intent(hisContext,PlayActivity.class);

        SingleItemmodel vMain = hisOneRowList.get(position);
        holder.history_lectureImg.setimageResource(vMain.getLectureImg());
        holder.history_lecturetopic.setText(vMain.getLecturetopic());
        holder.history_lectureInfo.setText(vMain.getLectureInfo());


        holder.history_vMenu.setonClickListener(v -> {
            PopupMenu popup = new PopupMenu(hisContext,holder.history_vMenu);
            popup.inflate(R.menu.history_menu_option);
            popup.setonMenuItemClickListener(item -> {
                switch (item.getItemId()){
                    case (R.id.history_watch):
                        Toast.makeText(hisContext,"starting playActivity..." + " " + position,Toast.LENGTH_LONG).show();
                        break;

                    case (R.id.history_remove):
                        //put all the codes for item "add to list" here
                        CourseActivityDatabase historyDB = new CourseActivityDatabase(hisContext);
                        sqliteDatabase db = historyDB.getWritableDatabase();
                        ContentValues historyValues = new ContentValues();
                        historyValues.put(DB_TABLES.LECTURE_HISTORY_FLAG,historyValues,new String[]{Integer.toString(vMain.getFlagId())});


                        Toast.makeText(hisContext,Toast.LENGTH_SHORT).show();
                        db.close();
                        hisOneRowList.remove(position);
                        notifyDataSetChanged();


                        break;
                    default:
                        break;
                }
                return true;
            });
            popup.show();
        });
        holder.history_card.setonClickListener(v ->{
            hisIntent.putExtra("historyItem",position);
            hisContext.startActivity(hisIntent);
        });
    }

我可能做错了什么? 我找不到我在这里做的逻辑,请有人帮助我

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?