Android studio 如何在更改sqlite中的数据的同时更改recycleview的内容?
我在recycleview(cardview的一部分,为每次插入生成)中有一个按钮,它在SQLite中增加一个id为的特定列,数据会被更改,但我必须重新启动活动才能更改。 更改后的数据必须显示在文本视图中 我对这一点还不熟悉,如何使它们动态变化Android studio 如何在更改sqlite中的数据的同时更改recycleview的内容?,android-studio,android-sqlite,android-recyclerview,Android Studio,Android Sqlite,Android Recyclerview,我在recycleview(cardview的一部分,为每次插入生成)中有一个按钮,它在SQLite中增加一个id为的特定列,数据会被更改,但我必须重新启动活动才能更改。 更改后的数据必须显示在文本视图中 我对这一点还不熟悉,如何使它们动态变化 public class attendence_recycleadapter extends RecyclerView.Adapter<attendence_recycleadapter.ViewHolder> { private
public class attendence_recycleadapter extends RecyclerView.Adapter<attendence_recycleadapter.ViewHolder> {
private List<attendence> mattendence_list;
private Context mcontext;
public RecyclerView mrecycleview;
@NonNull
@Override
public attendence_recycleadapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater= LayoutInflater.from(parent.getContext());
View v=inflater.inflate(R.layout.attendence_recycleview_card,parent,false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull attendence_recycleadapter.ViewHolder holder, final int position) {
final attendence attendence= mattendence_list.get(position);
holder.subname.setText("NAME: " + attendence.getSubname());
holder.subcredit.setText("CREDIT: " + attendence.getCredit());
holder.bunks.setText("BUNKS: " + attendence.getBunks());
holder.deletesub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Myhelper myhelper = new Myhelper(mcontext);
myhelper.delete(attendence.getId(),mcontext);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mattendence_list.size());
notifyDataSetChanged();
delete(position);
}
});
holder.addbunk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Myhelper myhelper = new Myhelper(mcontext);
myhelper.updatebunk(attendence.getId());
Toast.makeText(mcontext,"+1 class bunked",Toast.LENGTH_SHORT).show();
}
});
holder.deletebunk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Myhelper myhelper=new Myhelper(mcontext);
myhelper.updatebunkdelete(attendence.getId());
Toast.makeText(mcontext,"Bunk deleted",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mattendence_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView subname;
public TextView subcredit;
public TextView bunks;
public View layout;
public TextView addbunk;
public TextView deletebunk;
public TextView deletesub;
public ViewHolder(View itemView) {
super(itemView);
layout=itemView;
subname=(TextView)itemView.findViewById(R.id.attendernce_subname);
subcredit=(TextView)itemView.findViewById(R.id.attendernce_credit);
bunks=(TextView)itemView.findViewById(R.id.attendernce_bunks);
deletebunk =(TextView)itemView.findViewById(R.id.attendence_deletebunk);
addbunk = (TextView) itemView.findViewById(R.id.attendence_addbunk);
deletesub = (TextView) itemView.findViewById(R.id.attendence_deletesub);
}
}
public void add(int position,attendence attendence2){
mattendence_list.add(position,attendence2);
notifyItemInserted(position);
}
public void delete(int position){
mattendence_list.remove(position);
notifyItemRemoved(position);
}
public attendence_recycleadapter(List<attendence> mydataset, Context context, RecyclerView recyclerView){
mattendence_list = mydataset;
mcontext = context;
mrecycleview= recyclerView;
}
}
mysqlitehelper:
public class Myhelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="mydatabase.db";
public static final String TABLE_NAME="mytable";
public static final String COLUMN_ID="_id";
public static final String COL_1="NAME";
public static final String COL_2="CREDIT";
public static final String COL_3="BUNKS";
public static final String[] COLUMNS ={COLUMN_ID,COL_1,COL_2,COL_3};
Context con;
public Myhelper(Context context) {
super(context, DATABASE_NAME, null, 1);
con = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable= "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, " + " NAME TEXT, CREDIT TEXT, BUNKS INTEGER )";
db.execSQL(createTable);
Toast.makeText(con,"table created",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
onCreate(db);
}
public boolean insertData(String x,String y,int z){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, String.valueOf(x));
contentValues.put(COL_2,String.valueOf(y));
contentValues.put(COL_3,z);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public ArrayList<String> queryXdata(){
ArrayList<String> xnewdata= new ArrayList<String>();
String query = "SELECT "+ COL_1 + " FROM " + TABLE_NAME;
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor= db.rawQuery(query,null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
xnewdata.add(cursor.getString(cursor.getColumnIndex(COL_1)));
}
cursor.close();
return xnewdata;
}
public ArrayList<Integer> queryYdata(){
ArrayList<Integer> ynewdata= new ArrayList<Integer>();
String query = "SELECT "+ COL_3+ " FROM " + TABLE_NAME;
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor= db.rawQuery(query,null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
ynewdata.add(cursor.getInt(cursor.getColumnIndex(COL_3)));
}
cursor.close();
return ynewdata;
}
public ArrayList<Integer> queryZdata(){
ArrayList<Integer> znewdata= new ArrayList<Integer>();
String query = "SELECT "+ COL_2+ " FROM " + TABLE_NAME;
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor= db.rawQuery(query,null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
znewdata.add(((cursor.getColumnIndex(COL_2))));
}
cursor.close();
return znewdata;
}
public attendence getdetails(long id){
SQLiteDatabase db = this.getWritableDatabase();
String query= "SELECT" + COL_2+" FROM " + TABLE_NAME +" WHERE _id=" + id;
Cursor cursor= db.rawQuery(query,null);
attendence attendence1= new attendence();
if (cursor.getCount()>0){
cursor.moveToFirst();
attendence1.setSubname(cursor.getString(cursor.getColumnIndex(COL_1)));
attendence1.setCredit(cursor.getString(cursor.getColumnIndex(COL_2)));
attendence1.setBunks(cursor.getInt(cursor.getColumnIndex(COL_3)));
}
return attendence1;
}
public void delete(long id,Context context){
SQLiteDatabase db=this.getWritableDatabase();
db.execSQL("DELETE FROM "+TABLE_NAME+" WHERE _id='"+id+"'");
Toast.makeText(context,"delted",Toast.LENGTH_SHORT).show();
}
public List<attendence> list(String filter){
String query;
if (filter.equals("")){
query = "SELECT * FROM " + TABLE_NAME ;
}
else {
query= " SELECT * FROM " + TABLE_NAME ;
}
List<attendence> linkedlist = new LinkedList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
attendence attendence;
if (cursor.moveToFirst()){
do {
attendence = new attendence();
attendence.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
attendence.setSubname(cursor.getString(cursor.getColumnIndex(COL_1)));
attendence.setCredit(cursor.getString(cursor.getColumnIndex(COL_2)));
attendence.setBunks(cursor.getInt(cursor.getColumnIndex(COL_3)));
linkedlist.add(attendence);
}
while (cursor.moveToNext());
}
return linkedlist;
}
public int getbunk(long id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + " = " + String.valueOf(id), null);
int output = -1;
if (cursor != null) {
if (cursor.moveToFirst()) {
output = cursor.getInt(cursor.getColumnIndex(COL_3));
}
cursor.close();
}
return output;
}
public void updatebunk(long id){
SQLiteDatabase db = this.getWritableDatabase();
int bunk = getbunk(id);
int bunkinc= ++bunk;
ContentValues contentValues= new ContentValues();
contentValues.put(COL_3,bunkinc);
db.update(TABLE_NAME,contentValues,COLUMN_ID+ "=?",new String[]{String.valueOf(id)});
db.close();
}
public void updatebunkdelete(long id){
SQLiteDatabase db = this.getWritableDatabase();
int bunk = getbunk(id);
int bunkinc= --bunk;
ContentValues contentValues= new ContentValues();
contentValues.put(COL_3,bunkinc);
db.update(TABLE_NAME,contentValues,COLUMN_ID+ "=?",new String[]{String.valueOf(id)});
db.close();
}
}
公共类Myhelper扩展了SQLiteOpenHelper{
公共静态最终字符串数据库\u NAME=“mydatabase.db”;
公共静态最终字符串表\u NAME=“mytable”;
公共静态最终字符串列_ID=“_ID”;
公共静态最终字符串COL_1=“NAME”;
公共静态最终字符串COL_2=“CREDIT”;
公共静态最终字符串COL_3=“BUNKS”;
公共静态最终字符串[]列={COLUMN_ID,COL_1,COL_2,COL_3};
上下文con;
公共Myhelper(上下文){
super(上下文,数据库名称,null,1);
con=上下文;
}
@凌驾
public void onCreate(SQLiteDatabase db){
String createTable=“CREATE TABLE”+TABLE_NAME+”(“+列_ID+”整型主键自动递增“+”名称文本、信用文本、BUNKS整型)”;
execSQL(createTable);
Toast.makeText(con,“table created”,Toast.LENGTH_SHORT).show();
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
db.execSQL(“如果存在删除表”+表名称);
onCreate(db);
}
公共布尔插入数据(字符串x、字符串y、整数z){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues ContentValues=新ContentValues();
contentValues.put(COL_1,String.valueOf(x));
contentValues.put(COL_2,String.valueOf(y));
contentValues.put(COL_3,z);
long result=db.insert(表名称,null,contentValues);
如果(结果==-1){
返回false;
}否则{
返回true;
}
}
公共ArrayList queryXdata(){
ArrayList xnewdata=新的ArrayList();
String query=“从”+表名称中选择”+列1+;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(查询,空);
对于(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
添加(cursor.getString(cursor.getColumnIndex(COL_1)));
}
cursor.close();
返回xnewdata;
}
公共ArrayList QueryData(){
ArrayList ynewdata=新的ArrayList();
String query=“从”+表名称中选择”+列3+;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(查询,空);
对于(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
添加(cursor.getInt(cursor.getColumnIndex(COL_3));
}
cursor.close();
返回ynewdata;
}
公共ArrayList queryZdata(){
ArrayList znewdata=新的ArrayList();
String query=“从”+表名称中选择”+列2+;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(查询,空);
对于(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
znewdata.add((cursor.getColumnIndex(COL_2)));
}
cursor.close();
返回znewdata;
}
公众参与详细信息(长id){
SQLiteDatabase db=this.getWritableDatabase();
String query=“从“+表格名称+”中选择“+COL_2+”,其中_id=“+id;
Cursor Cursor=db.rawQuery(查询,空);
注意力注意力1=新注意力();
if(cursor.getCount()>0){
cursor.moveToFirst();
attendence1.setSubname(cursor.getString(cursor.getColumnIndex(COL_1));
attendence1.setCredit(cursor.getString(cursor.getColumnIndex(COL_2));
注意1.缩进(cursor.getInt(cursor.getColumnIndex(COL_3));
}
返回出席人数1;
}
公共无效删除(长id,上下文){
SQLiteDatabase db=this.getWritableDatabase();
db.execSQL(“从“+表名+”中删除,其中“+id=”“+id+””);
Toast.makeText(上下文,“delted”,Toast.LENGTH_SHORT).show();
}
公共列表(字符串筛选器){
字符串查询;
if(filter.equals(“”){
query=“SELECT*FROM”+表名称;
}
否则{
query=“SELECT*FROM”+表名称;
}
List linkedlist=新建linkedlist();
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(查询,空);
注意力;
if(cursor.moveToFirst()){
做{
注意力=新注意力();
attendence.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_ID));
attendence.setSubname(cursor.getString(cursor.getColumnIndex(COL_1));
attendence.setCredit(cursor.getString(cursor.getColumnIndex(COL_2));
注意力.退步(cursor.getInt(cursor.getColumnIndex(COL_3));
linkedlist.add(出席);
}
while(cursor.moveToNext());
}
返回linkedlist;
}
公共上铺(长id){
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(“从“+表名+”中选择*,其中“+列ID+”=”+字符串.valueOf(ID),null);
int输出=-1;
如果(光标!=null){
if(cursor.moveToFirst()){
输出=cursor.getInt(cursor.getColumnIndex(COL_3));
}
cursor.close();
}
返回输出;
}
public void updatebunk(长id){
SQLiteDatabase db=this.getWritableDatabase();
int bunk=getbunk(id);
int bunkinc=++bunk;
ContentValues ContentValues=新ContentValues();
contentValues.put(COL_3,bunkinc);
数据库更新(表1)_
public class Myhelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="mydatabase.db";
public static final String TABLE_NAME="mytable";
public static final String COLUMN_ID="_id";
public static final String COL_1="NAME";
public static final String COL_2="CREDIT";
public static final String COL_3="BUNKS";
public static final String[] COLUMNS ={COLUMN_ID,COL_1,COL_2,COL_3};
Context con;
public Myhelper(Context context) {
super(context, DATABASE_NAME, null, 1);
con = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable= "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, " + " NAME TEXT, CREDIT TEXT, BUNKS INTEGER )";
db.execSQL(createTable);
Toast.makeText(con,"table created",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
onCreate(db);
}
public boolean insertData(String x,String y,int z){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, String.valueOf(x));
contentValues.put(COL_2,String.valueOf(y));
contentValues.put(COL_3,z);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public ArrayList<String> queryXdata(){
ArrayList<String> xnewdata= new ArrayList<String>();
String query = "SELECT "+ COL_1 + " FROM " + TABLE_NAME;
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor= db.rawQuery(query,null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
xnewdata.add(cursor.getString(cursor.getColumnIndex(COL_1)));
}
cursor.close();
return xnewdata;
}
public ArrayList<Integer> queryYdata(){
ArrayList<Integer> ynewdata= new ArrayList<Integer>();
String query = "SELECT "+ COL_3+ " FROM " + TABLE_NAME;
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor= db.rawQuery(query,null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
ynewdata.add(cursor.getInt(cursor.getColumnIndex(COL_3)));
}
cursor.close();
return ynewdata;
}
public ArrayList<Integer> queryZdata(){
ArrayList<Integer> znewdata= new ArrayList<Integer>();
String query = "SELECT "+ COL_2+ " FROM " + TABLE_NAME;
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor= db.rawQuery(query,null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
znewdata.add(((cursor.getColumnIndex(COL_2))));
}
cursor.close();
return znewdata;
}
public attendence getdetails(long id){
SQLiteDatabase db = this.getWritableDatabase();
String query= "SELECT" + COL_2+" FROM " + TABLE_NAME +" WHERE _id=" + id;
Cursor cursor= db.rawQuery(query,null);
attendence attendence1= new attendence();
if (cursor.getCount()>0){
cursor.moveToFirst();
attendence1.setSubname(cursor.getString(cursor.getColumnIndex(COL_1)));
attendence1.setCredit(cursor.getString(cursor.getColumnIndex(COL_2)));
attendence1.setBunks(cursor.getInt(cursor.getColumnIndex(COL_3)));
}
return attendence1;
}
public void delete(long id,Context context){
SQLiteDatabase db=this.getWritableDatabase();
db.execSQL("DELETE FROM "+TABLE_NAME+" WHERE _id='"+id+"'");
Toast.makeText(context,"delted",Toast.LENGTH_SHORT).show();
}
public List<attendence> list(String filter){
String query;
if (filter.equals("")){
query = "SELECT * FROM " + TABLE_NAME ;
}
else {
query= " SELECT * FROM " + TABLE_NAME ;
}
List<attendence> linkedlist = new LinkedList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
attendence attendence;
if (cursor.moveToFirst()){
do {
attendence = new attendence();
attendence.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
attendence.setSubname(cursor.getString(cursor.getColumnIndex(COL_1)));
attendence.setCredit(cursor.getString(cursor.getColumnIndex(COL_2)));
attendence.setBunks(cursor.getInt(cursor.getColumnIndex(COL_3)));
linkedlist.add(attendence);
}
while (cursor.moveToNext());
}
return linkedlist;
}
public int getbunk(long id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + " = " + String.valueOf(id), null);
int output = -1;
if (cursor != null) {
if (cursor.moveToFirst()) {
output = cursor.getInt(cursor.getColumnIndex(COL_3));
}
cursor.close();
}
return output;
}
public void updatebunk(long id){
SQLiteDatabase db = this.getWritableDatabase();
int bunk = getbunk(id);
int bunkinc= ++bunk;
ContentValues contentValues= new ContentValues();
contentValues.put(COL_3,bunkinc);
db.update(TABLE_NAME,contentValues,COLUMN_ID+ "=?",new String[]{String.valueOf(id)});
db.close();
}
public void updatebunkdelete(long id){
SQLiteDatabase db = this.getWritableDatabase();
int bunk = getbunk(id);
int bunkinc= --bunk;
ContentValues contentValues= new ContentValues();
contentValues.put(COL_3,bunkinc);
db.update(TABLE_NAME,contentValues,COLUMN_ID+ "=?",new String[]{String.valueOf(id)});
db.close();
}
}
public int updatebunk(long id){
SQLiteDatabase db = this.getWritableDatabase();
int bunk = getbunk(id);
int bunkinc= ++bunk;
ContentValues contentValues= new ContentValues();
contentValues.put(COL_3, bunkinc);
db.update(TABLE_NAME,contentValues,COLUMN_ID+ "=?",new String[]{String.valueOf(id)});
db.close();
return bunkinc;
}
holder.addbunk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Myhelper myhelper = new Myhelper(mcontext);
int newbunk = myhelper.updatebunk(attendence.getId());
attendence.setBunks(newbunk);
mattendence_list.set(position, attendence);
notifyDataSetChanged();
Toast.makeText(mcontext,"+1 class bunked",Toast.LENGTH_SHORT).show();
}
});