从RecyclerView删除房间条目

时间:2018-03-22 17:07:07

标签: android-studio android-recyclerview dao delete-row android-room

我是编程新手,我跟着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));
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

DatabaseActivity:

adapter = new StudentAdapter(db, students);

<强>已更新

适配器类中:

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);
            }
        });
}