android – 从sqlite删除项目后,RecyclerView不会更新?

android – 从sqlite删除项目后,RecyclerView不会更新?,第1张

概述我已经从sqlite填充了recyclerview.当点击每一行时,行将从sqlite中删除,但recyclelerview在删除后没有显示更新列表. Recycler视图仅在再次启动活动后显示更新列表.我的问题是如何在不刷新的情况下从recylcerview中删除项目后立即更新回收站视图. 以下是我的代码 SecondActivity.java import android.support.v7 我已经从sqlite填充了recyclervIEw.当点击每一行时,行将从sqlite中删除,但recyclelervIEw在删除后没有显示更新列表. Recycler视图仅在再次启动活动后显示更新列表.我的问题是如何在不刷新的情况下从recylcervIEw中删除项目后立即更新回收站视图.

以下是我的代码

SecondActivity.java

@R_403_5565@ androID.support.v7.app.AppCompatActivity;@R_403_5565@ androID.os.Bundle;@R_403_5565@ androID.support.v7.Widget.linearlayoutmanager;@R_403_5565@ androID.support.v7.Widget.RecyclerVIEw;@R_403_5565@ androID.support.v7.Widget.Toolbar;@R_403_5565@ androID.vIEw.Menu;@R_403_5565@ androID.vIEw.MenuItem;@R_403_5565@ java.util.ArrayList;@R_403_5565@ java.util.List;public class SecondActivity extends AppCompatActivity {    DatabaseHelpher helpher;    List<DatabaseModel> dbList;RecyclerVIEw mRecyclerVIEw;    private RecyclerVIEw.Adapter mAdapter;    private RecyclerVIEw.LayoutManager mLayoutManager;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_second);        Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar);        setSupportActionbar(toolbar);        getSupportActionbar().setdisplayHomeAsUpEnabled(true);        helpher = new DatabaseHelpher(this);        dbList= new ArrayList<DatabaseModel>();        dbList = helpher.getDataFromDB();        mRecyclerVIEw = (RecyclerVIEw)findVIEwByID(R.ID.recyclevIEw);        mRecyclerVIEw.setHasFixedSize(true);        // use a linear layout manager        mLayoutManager = new linearlayoutmanager(this);        mRecyclerVIEw.setLayoutManager(mLayoutManager);        // specify an adapter (see also next example)        mAdapter = new RecyclerAdapter(this,dbList);        mRecyclerVIEw.setAdapter(mAdapter);mAdapter.notifyDataSetChanged ();    }    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_second,menu);        return true;    }    @OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        switch (item.getItemID()) {            case androID.R.ID.home:                finish();                return true;        }        return super.onoptionsItemSelected(item);    }}

DatabaseHelpher.java

@R_403_5565@ androID.content.ContentValues;@R_403_5565@ androID.content.Context;@R_403_5565@ androID.database.Cursor;@R_403_5565@ androID.database.sqlite.sqliteDatabase;@R_403_5565@ androID.database.sqlite.sqliteOpenHelper;@R_403_5565@ androID.util.Log;@R_403_5565@ androID.Widget.Toast;@R_403_5565@ java.util.ArrayList;@R_403_5565@ java.util.List;public class DatabaseHelpher extends sqliteOpenHelper {    private static final String DATABASE_name="student";    private static final int DATABASE_VERSION = 1;    private static final String STUDENT_table = "stureg";    private static final String STU_table = "create table "+STUDENT_table +"(name TEXT,email TEXT primary key,roll TEXT,address TEXT,branch TEXT)";Context context;    public DatabaseHelpher(Context context) {        super(context,DATABASE_name,null,DATABASE_VERSION);        this.context = context;    }    @OverrIDe    public voID onCreate(sqliteDatabase db) {        db.execsql(STU_table);    }    @OverrIDe    public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {        db.execsql("DROP table IF EXISTS " + STUDENT_table);        // Create tables again        onCreate(db);    }/* Insert into database*/    public voID insertIntoDB(String name,String email,String roll,String address,String branch){        Log.d("insert","before insert");        // 1. get reference to writable DB        sqliteDatabase db = this.getWritableDatabase();        // 2. create ContentValues to add key "column"/value        ContentValues values = new ContentValues();        values.put("name",name);        values.put("email",email);        values.put("roll",roll);         values.put("address",address);        values.put("branch",branch);        // 3. insert        db.insert(STUDENT_table,values);        // 4. close        db.close();        Toast.makeText(context,"insert value",Toast.LENGTH_LONG);        Log.i("insert into DB","After insert");    }/* Retrive  data from database */    public List<DatabaseModel> getDataFromDB(){        List<DatabaseModel> modelList = new ArrayList<DatabaseModel>();        String query = "select * from "+STUDENT_table;        sqliteDatabase db = this.getWritableDatabase();Cursor cursor = db.rawquery(query,null);        if (cursor.movetoFirst()){            do {                DatabaseModel model = new DatabaseModel();                model.setname(cursor.getString(0));                model.setEmail(cursor.getString(1));                model.setRoll(cursor.getString(2));                model.setAddress(cursor.getString(3));                model.setBranch(cursor.getString(4));                modelList.add(model);            }while (cursor.movetoNext());        }        Log.d("student data",modelList.toString());        return modelList;    }    /*delete a row from database*/    public voID deleteARow(String email){        sqliteDatabase db= this.getWritableDatabase();        db.delete(STUDENT_table,"email" + " = ?",new String[] { email });        db.close();    }}

DatabaseModel.java

public class DatabaseModel {    private String name;    private String roll;    private String address;    private String branch;    private String email;    public String getname() {        return name;    }    public voID setname(String name) {        this.name = name;    }    public String getRoll() {        return roll;    }    public voID setRoll(String roll) {        this.roll = roll;    }    public String getAddress() {        return address;    }    public voID setAddress(String address) {        this.address = address;    }    public String getBranch() {        return branch;    }    public voID setBranch(String branch) {        this.branch = branch;    }    public String getEmail() {        return email;    }    public voID setEmail(String email) {        this.email = email;    }}

RecyclerAdapter.java

@R_403_5565@ androID.content.Context;@R_403_5565@ androID.content.Intent;@R_403_5565@ androID.os.Bundle;@R_403_5565@ androID.support.v7.Widget.RecyclerVIEw;@R_403_5565@ androID.vIEw.LayoutInflater;@R_403_5565@ androID.vIEw.VIEw;@R_403_5565@ androID.vIEw.VIEwGroup;@R_403_5565@ androID.Widget.TextVIEw;@R_403_5565@ androID.Widget.Toast;@R_403_5565@ java.util.ArrayList;@R_403_5565@ java.util.List;public class RecyclerAdapter extends RecyclerVIEw.Adapter<RecyclerAdapter.VIEwHolder> {  static   List<DatabaseModel> dbList;    static  Context context;    static DatabaseHelper dh;    RecyclerAdapter(Context context,List<DatabaseModel> dbList ){        this.dbList = new ArrayList<DatabaseModel>();        this.context = context;        this.dbList = dbList;dh=new DatabaseHelper(context);    }    @OverrIDe    public RecyclerAdapter.VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {        VIEw itemLayoutVIEw = LayoutInflater.from(parent.getContext()).inflate(                R.layout.item_row,null);        // create VIEwHolder        VIEwHolder vIEwHolder = new VIEwHolder(itemLayoutVIEw);        return vIEwHolder;    }    @OverrIDe    public voID onBindVIEwHolder(RecyclerAdapter.VIEwHolder holder,int position) {        holder.name.setText(dbList.get(position).getname());        holder.email.setText(dbList.get(position).getEmail());    }    @OverrIDe    public int getItemCount() {        return dbList.size();    }    public static class VIEwHolder extends RecyclerVIEw.VIEwHolder implements VIEw.OnClickListener {        public TextVIEw name,email;        public VIEwHolder(VIEw itemLayoutVIEw) {            super(itemLayoutVIEw);            name = (TextVIEw) itemLayoutVIEw                    .findVIEwByID(R.ID.rvname);            email = (TextVIEw)itemLayoutVIEw.findVIEwByID(R.ID.rvemail);            itemLayoutVIEw.setonClickListener(this);        }        @OverrIDe        public voID onClick(VIEw v) {           dh.delete(dbList.get(getAdapterposition()).getEmail);            Toast.makeText(RecyclerAdapter.context,"you have clicked Row " + getAdapterposition(),Toast.LENGTH_LONG).show();        }    }}

我试图使用mAdapter.notifyDataSetChanged()更新recyclervIEw;但它对我不起作用.请给我打电话.谢谢

解决方法 从arrayList和数据库中删除数据,然后通知您的列表将更新.

要么

从数据库中删除数据,删除后从数据库中获取数据并使用新数据重新加载arrayList将完成您的工作

dbList.remove(getAdapterposition());notifyDataSetChanged();//or use this for better perfomance.notifyItemRemoved(getAdapterposition());

在删除方法中传递位置

总结

以上是内存溢出为你收集整理的android – 从sqlite删除项目后,RecyclerView不会更新?全部内容,希望文章能够帮你解决android – 从sqlite删除项目后,RecyclerView不会更新?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1122741.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存