gpt4 book ai didi

android-studio - 从 RecyclerView 中删除房间条目

转载 作者:行者123 更新时间:2023-12-03 17:59:34 25 4
gpt4 key购买 nike

我是编程新手,我关注了 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));
}
});
}
}

最佳答案

数据库事件:

adapter = new StudentAdapter(db, students);

更新

在您的 适配器 类(class):
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);
}
});
}

关于android-studio - 从 RecyclerView 中删除房间条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49434525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com