我已经从sqlite填充了recyclerview。当点击每一行时,行将从sqlite中删除,但recyclelerview在删除后没有显示更新列表。 Recycler视图仅在再次启动活动后显示更新列表。我的问题是如何在不刷新的情况下从recylcerview中删除项目后立即更新回收站视图。
以下是我的代码
SecondActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import 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
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import 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, null, 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
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import 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,但它对我不起作用。请给我打电话。谢谢你
答案 0 :(得分:6)
从arraylist和数据库中删除数据,并通知您的列表将更新。
或
从数据库中删除数据,删除后从数据库中获取数据并使用新数据重新加载arraylist将完成您的工作
dbList.remove(getAdapterPosition());
notifyDataSetChanged();
//or use this for better perfomance.
notifyItemRemoved(getAdapterPosition());
在删除方法中传递位置
答案 1 :(得分:2)
您只需要在onClick
方法
@Override
public void onClick(View v) {
notifyItemRemoved(this.getLayoutPosition());
}
答案 2 :(得分:2)
在sqlite中更改数据时,有一种完全不同的方法来更新recyclerview。这是通过使用游标。
您可能想看以下视频:https://www.youtube.com/playlist?list=PLrnPJCHvNZuBMJmll0xy2L2McYInT3aiu
答案 3 :(得分:1)
您正在从数据库中删除数据,但不是从您获取所有数据的列表中删除数据,因此,即使您从列表中删除数据,它也会显示,即使它已被删除。因此,您需要更改删除方法,还需要从列表中删除数据。
检查以下更新的删除方法。
@Override
public void onClick(View v) {
dh.delete(dbList.get(getAdapterPosition()).getEmail);
// These two lines added for remove data from adapter also.
dbList.remove(getAdapterPosition());
notifyDataSetChanged();
Toast.makeText(RecyclerAdapter.context, "you have clicked Row " + getAdapterPosition(), Toast.LENGTH_LONG).show();
}
答案 4 :(得分:1)
//在Adapter类中定义一个接口
private static ItemClickListener itemClickListener;
public interface ItemClickListener {
void onItemClick(int position, View v);
}
和方法
public void setOnItemClickListener(ItemClickListener myClickListener) {
RecyclerAdapter.itemClickListener = myClickListener;
}
在viewHolder的onClick方法
@Override
public void onClick(View v) {
dh.delete(dbList.remove(getAdapterPosition()).getEmail);
itemClickListener.onItemClick(getLayoutPosition(), v);
Toast.makeText(RecyclerAdapter.context, "you have clicked Row " + getAdapterPosition(), Toast.LENGTH_LONG).show();
}
在您的Activity的onCreate()或onResume()方法
中 mAdapter.setOnItemClickListener(new RecyclerAdapter.ItemClickListener() {
@Override
public void onItemClick(int position, View v) {
mAdapter.notifyDataSetChanged();
}
});
答案 5 :(得分:1)
尝试重新加载信息。
更改此
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
对此:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
dbList = helpher.getDataFromDB();
mAdapter = new RecyclerAdapter(this,dbList);
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged ();
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
每次点击都会在您的Recycler Vuew上重新加载信息。
答案 6 :(得分:0)
使用
从arrayList中删除列表数据 arrayList.remove(position);
使用
更新您的适配器 mAdapter.notifyDataSetChanged ();
答案 7 :(得分:0)
使用notifyItemRemoved()
代替notifyDataSetChanged()
答案 8 :(得分:0)
像这样
builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PersonDBHelper dbHelper = new PersonDBHelper(mContext);
dbHelper.deletePersonRecord(person.getId(), mContext);
mPeopleList.remove(position);
mRecyclerV.removeViewAt(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mPeopleList.size());
notifyDataSetChanged();
}
});
并在回收列表中使用“重启时使用”方法
答案 9 :(得分:0)
您可以在活动中执行删除操作后调用recreate(),以便刷新活动。这个对我有用。希望这也会对您有用。