从回收站视图和 sqlite 中删除一行

Posted

技术标签:

【中文标题】从回收站视图和 sqlite 中删除一行【英文标题】:Deleting a row from recycler view and sqlite 【发布时间】:2020-06-28 06:45:43 【问题描述】:

这是我的适配器类:

public class adapter_cgpa extends RecyclerView.Adapter<adapter_cgpa.Viewholder> 

    ArrayList<POJO> cgpaArrayList;


    public adapter_cgpa(ArrayList<POJO> cgpaArrayList) 
        this.cgpaArrayList = cgpaArrayList;
    

    @NonNull
    @Override
    public adapter_cgpa.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 

        View listitem = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_cgpa, parent, false);
        return new Viewholder(listitem);

    

    @Override
    public void onBindViewHolder(@NonNull adapter_cgpa.Viewholder holder, int position) 

        POJO cgpa= cgpaArrayList.get(position);
        holder.cname.setText(cgpa.getCname());
        holder.no_of_sems.setText(cgpa.getNo_of_sems());
        holder.cgpa.setText(cgpa.getCgpa());
        holder.percentage.setText(cgpa.getPercentage());
        holder.schemec.setText(cgpa.getSchemec());


    

    @Override
    public int getItemCount() 
        return cgpaArrayList.size();
    

    public class Viewholder extends RecyclerView.ViewHolder 

        TextView cname, no_of_sems, cgpa, percentage,schemec;
        ImageButton btndelete2;


        public Viewholder(@NonNull View itemView) 
            super(itemView);

            cname=(TextView)itemView.findViewById(R.id.name);
            no_of_sems=(TextView)itemView.findViewById(R.id.no_of_sem);
            cgpa=(TextView)itemView.findViewById(R.id.textView49);
            percentage=(TextView)itemView.findViewById(R.id.textView55);
            schemec=(TextView)itemView.findViewById(R.id.scheme2);
            btndelete2=(ImageButton)itemView.findViewById(R.id.btndelete2);

            btndelete2.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 

                    final String snc = cname.getText().toString();
                    final String semrc = no_of_sems.getText().toString();
                    final String schc = schemec.getText().toString();

                    dbmanager db= new dbmanager(v.getContext());
                    db.delete2(snc,semrc,schc);
                
            );

        



    


这是我的 dbmanager 类:

public class dbmanager extends SQLiteOpenHelper 

    String sgpa_table="create table Sgpa (sname text, semester text, sgpa  text,percent text, schemes text, primary key(sname,semester,schemes))";
    String cgpa_table="create table Cgpa (cname text, no_of_sems int, cgpa text, percentage text, schemec text, primary key(cname,no_of_sems,schemec))";

    public dbmanager(@Nullable Context context) 
        super(context, "Student", null, 1);
    

    @Override
    public void onCreate(SQLiteDatabase db) 
        db.execSQL(sgpa_table);
        db.execSQL(cgpa_table);

    

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 

    



    public Cursor fetch_data1() 
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "Select*FROM Sgpa" ;
        Cursor cursor = db.rawQuery(query,null);

        return cursor;
    

    public Cursor fetch_data2() 
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "Select*FROM Cgpa" ;
        Cursor cursor = db.rawQuery(query,null);
//        if (cursor != null) 
//            cursor.moveToFirst();
//        
        return cursor;

    


    public boolean delete2(String snc, String semrc, String schc)
        SQLiteDatabase db=this.getWritableDatabase();
        int result = db.delete("Cgpa", "cname= ? and no_of_sems = ? and schemec = ?", new String[] snc, semrc, schc);
        return result > 0;
    


这不起作用。我认为不是在 textview 中传递值,而是需要传递项目位置,即行 id 和列 id,但我不知道该怎么做。有人可以帮我解决这个问题吗?

【问题讨论】:

如果您包含,您遇到什么错误或任何异常堆栈跟踪以更好地解决您的问题,那就没问题 没有错误。删除功能不起作用。我可以在调试时看到 snc、semrc 和 schc(参数)正在存储空白值。并且没有任何内容被删除。这可能是因为回收站视图具有相同的文本视图和图像按钮重复的卡片视图。 删除功能不起作用或删除后列表未更新。删除后可能没有刷新 UI。尝试在适配器中调用 notifyChanged 进行检查 【参考方案1】:

cgpaArrayList.remove(position);删除后必须添加notifyDataSetChanged();

编辑

  public class adapter_cgpa extends RecyclerView.Adapter<adapter_cgpa.Viewholder> 

        ArrayList<POJO> cgpaArrayList;


        public adapter_cgpa(ArrayList<POJO> cgpaArrayList) 
            this.cgpaArrayList = cgpaArrayList;
        

        @NonNull
        @Override
        public adapter_cgpa.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 

            View listitem = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_cgpa, parent, false);
            return new Viewholder(listitem);

        

        @Override
        public void onBindViewHolder(@NonNull adapter_cgpa.Viewholder holder, final int position) 

            POJO cgpa = cgpaArrayList.get(position);
            holder.cname.setText(cgpa.getCname());
            holder.no_of_sems.setText(cgpa.getNo_of_sems());
            holder.cgpa.setText(cgpa.getCgpa());
            holder.percentage.setText(cgpa.getPercentage());
            holder.schemec.setText(cgpa.getSchemec());
            btndelete2.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 

                    delete (position);

                
            );

        

        @Override
        public int getItemCount() 
            return cgpaArrayList.size();
        

        public class Viewholder extends RecyclerView.ViewHolder 

            TextView cname, no_of_sems, cgpa, percentage, schemec;
            ImageButton btndelete2;


            public Viewholder(@NonNull View itemView) 
                super(itemView);

                cname = (TextView) itemView.findViewById(R.id.name);
                no_of_sems = (TextView) itemView.findViewById(R.id.no_of_sem);
                cgpa = (TextView) itemView.findViewById(R.id.textView49);
                percentage = (TextView) itemView.findViewById(R.id.textView55);
                schemec = (TextView) itemView.findViewById(R.id.scheme2);
                btndelete2 = (ImageButton) itemView.findViewById(R.id.btndelete2);


            


        
        public void delete(final int position) 
            cgpaArrayList.remove(position);
            notifyDataSetChanged();
            final String snc = cname.getText().toString();
            final String semrc = no_of_sems.getText().toString();
            final String schc = schemec.getText().toString();

            dbmanager db = new dbmanager(v.getContext());
            db.delete2(snc, semrc, schc);
        

    

希望对你有帮助。

【讨论】:

如何在其中声明位置? 您似乎没有将代码放在适当的方法中。

以上是关于从回收站视图和 sqlite 中删除一行的主要内容,如果未能解决你的问题,请参考以下文章

使用 sqlite kotlin 的回收站视图

在复选框状态更改时从回收器视图适配器更新 sqlite 数据库

从 SQLITE 回收器视图获取图像路径错误

具有重复值的回收站更新

如何使用光标和循环显示来自 sqlite 的片段的 recyclerview

将 json 数据存储到 sqlite 并检索到回收站视图