In my Room Sqlite database adapter, I have a image button defined as:
public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.ViewHolder> {
List<PlaceSaved> items;
public PlacesAdapter(List<PlaceSaved> items) {
this.items = items;
}
@Override
public PlacesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.places_list_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(PlacesAdapter.ViewHolder holder, int position) {
holder.name.setText(items.get(position).getTitle());
holder.time.setText(items.get(position).getTime());
holder.delbutton.setClickable(true);
}
@Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView name;
public TextView time;
public ImageButton delbutton;
public ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.secondLine);
time= itemView.findViewById(R.id.firstLine);
delbutton = itemView.findViewById(R.id.delicon);
}
}
}
with delicon
is defined in places_list_item.xml
<TextView
android:id="@+id/secondLine"/>
<TextView
android:id="@+id/firstLine"/>
<ImageButton
android:id="@+id/delicon"/>
Now, I am trying to delete each the corresponding row by clicking the imagebutton. I have defined the DBinterface as:
@Dao
public interface DatabaseInterface {
@Query("SELECT * FROM placesaved")
List<PlaceSaved> getAllItems();
@Insert
void insertAll(PlaceSaved... todoListItems);
@Delete
void delete(PlaceSaved todeListItems);
}
and now I am stuck. How and where I should call the delete function to delete the row? Kindly help.
Also, dont know if this is required, the list is defined as:
@Entity
public class PlaceSaved {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "time")
private String time;
@ColumnInfo(name="title")
private String title;
public PlaceSaved(){
}
public PlaceSaved(String time, String title) {
this.time = time;
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}