从 RecyclerView 中删除房间条目
Posted
技术标签:
【中文标题】从 RecyclerView 中删除房间条目【英文标题】:Deleting Room entry from RecyclerView 【发布时间】:2018-09-01 05:41:25 【问题描述】:我是编程新手,我跟随this tutorial 使用适配器创建了RecyclerView
。我不知道如何使用我的 Dao 中的删除功能与每个条目旁边的 ImageButton.
相关。另外,这是我的第一个应用程序,所以当您看到“.allowMainThreadQueries”之类的内容时,请多多关照。
带有 recyclerview 的活动:
public class DatabaseActivity extends AppCompatActivity
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
FloatingActionButton fab;
ImageButton delete;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database);
recyclerView = findViewById(R.id.recycler_view);
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "production").allowMainThreadQueries().build();
final List<Student> students = db.studentDao().getAllUsers();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new StudentAdapter(students);
recyclerView.setAdapter(adapter);
fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
startActivity(new Intent(DatabaseActivity.this, DatabaseAddActivity.class));
);
适配器:
class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder>
List<Student> students;
public StudentAdapter(List<Student> students)
this.students = students;
@Override
public StudentAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_row, parent, false);
return new ViewHolder(view);
@Override
public void onBindViewHolder(StudentAdapter.ViewHolder holder, int position)
holder.first_name.setText(students.get(position).getFirstName());
holder.last_name.setText(students.get(position).getLastName());
holder.email.setText(students.get(position).getEmail());
//See how many items need to be displayed
@Override
public int getItemCount()
return students.size();
public class ViewHolder extends RecyclerView.ViewHolder
public TextView first_name;
public TextView last_name;
public TextView email;
public ImageButton delete;
//What we are showing in the viewholder
public ViewHolder(View itemView)
super(itemView);
first_name = itemView.findViewById(R.id.first_name);
last_name = itemView.findViewById(R.id.last_name);
email = itemView.findViewById(R.id.email);
delete = itemView.findViewById(R.id.delete);
道:
@Dao
public interface StudentDao
@Query("SELECT * FROM student ")
List<Student> getAllUsers();
@Query("SELECT * FROM student WHERE monday = 1")
List<Student> getMonday();
@Query("SELECT * FROM student WHERE tuesday = 1")
List<Student> getTuesday();
@Query("SELECT * FROM student WHERE wednesday = 1")
List<Student> getWednesday();
@Query("SELECT * FROM student WHERE thursday = 1")
List<Student> getThursday();
@Query("SELECT * FROM student WHERE friday = 1")
List<Student> getFriday();
@Insert
void insertAll(Student... students);
@Delete
void delete(Student student);
@Update
void updateStudent(Student student);
学生地址:
public class DatabaseAddActivity extends AppCompatActivity
//Calling needed resources
EditText firstName;
EditText lastName;
EditText email;
Button button;
CheckBox monday, tuesday, wednesday, thursday, friday;
//Defining which view.xml file to use
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database_add);
//Using resources to find using .xml setup
firstName = findViewById(R.id.first_name);
lastName = findViewById(R.id.last_name);
email = findViewById(R.id.email);
monday = findViewById(R.id.monday_switch);
tuesday = findViewById(R.id.tuesday_switch);
wednesday = findViewById(R.id.wednesday_switch);
thursday = findViewById(R.id.thursday_switch);
friday = findViewById(R.id.friday_switch);
button = findViewById(R.id.button_add);
//What happens when button is pressed
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "production").allowMainThreadQueries().build();
Boolean mondayState = monday.isChecked();
Boolean tuesdayState = tuesday.isChecked();
Boolean wednesdayState = wednesday.isChecked();
Boolean thursdayState = thursday.isChecked();
Boolean fridayState = friday.isChecked();
Student student = new Student(firstName.getText().toString(), lastName.getText().toString(), email.getText().toString(), mondayState, tuesdayState, wednesdayState, thursdayState, fridayState);
db.studentDao().insertAll(student);
startActivity(new Intent(DatabaseAddActivity.this,DatabaseActivity.class));
);
【问题讨论】:
您能出示您的Dao
代码吗?你有没有看过:developer.android.com/reference/android/arch/persistence/room/…
就是这样。我确实看过,但仍然没有线索。
【参考方案1】:
在DatabaseActivity:
adapter = new StudentAdapter(db, students);
更新
在您的 Adapter 类中:
AppDatabase db;
List<Student> students;
public StudentAdapter(AppDatabase db, List<Student> students)
this.db = db;
this.students = students;
@Override
public void onBindViewHolder(StudentAdapter.ViewHolder holder, int position)
Student student = students.get(position);
holder.first_name.setText(students.get(position).getFirstName());
holder.last_name.setText(students.get(position).getLastName());
holder.email.setText(students.get(position).getEmail());
holder.delete.setOnClickListener.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
db.delete(student);
students.remove(student);
notifyItemRemoved(position);
);
【讨论】:
我试过这个。它没有删除,所以我检查了日志,它说“没有向 HAL 提供足够的数据,预期位置 11857215 ,只写了 11856960”。我不知道这是否重要,但是是的。此外,新条目现在会覆盖旧条目 @Philedor 向您的学生展示在活动中添加代码? 添加到主评论 @Philedor 我忘了在删除onClick()
方法中在db.delete(student);
之后添加这个笔划students.remove(student);
以上是关于从 RecyclerView 中删除房间条目的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView上下拖动条目排序,左右划出屏幕删除条目的最简单的实现
将回收站视图数据中的 onMove() 更改更新到房间数据库