I have an activity that reads data from firebase database and displays the data in a firebase list adapter. Each item in the list view has 2 edit text and a button for deleting the item from both the list view and the firebase database. Below is the code for displaying the firebase list adapter
adapter =new FirebaseListAdapter <TravelDetails>
( options){
protected void populateView(View v, TravelDetails model, int position) {
Button delete=(Button)v.findViewById(R.id.button_accept);
final TextView z=(TextView)v.findViewById(R.id.dropoff);
final TextView text=(TextView)v.findViewById(R.id.txref);
text.setText(model.getTxt_Ref());
z.setText(model.getDropoffspot());
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {.....
The list view works fine. Data is loaded from firebase, but I want to be able to use this button delete to delete items from both the list view and firebase database. I did research and only found ways on item click and not using a button, an example is this Android studio remove items from both listview and firebase but I don't know how to apply the principle to the button associated with the item from the database. Kindly render assistance
Below is the code of my adapter class public class TravelDetails {
private String dropoffspot;
private String txt_Ref;
public TravelDetails(){
}
public String getDropoffspot(){
return dropoffspot;
}
public void setDropoffspot(String dropoffspot){
this.dropoffspot=dropoffspot;
}
public String getTxt_Ref() {
return txt_Ref;
}
public void setTxt_Ref(String txt_Ref) {
this.txt_Ref = txt_Ref;
}
}
You can do this way inside your adapter
// make this function inside your adapter
protected void populateView(View v, ArrayList<TravelDetails> models, final int position) {
Button delete=(Button)v.findViewById(R.id.button_accept);
final TextView z=(TextView)v.findViewById(R.id.dropoff);
final TextView text=(TextView)v.findViewById(R.id.txref);
text.setText(model.getTxt_Ref());
z.setText(model.getDropoffspot());
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// code to delete from firebase
models.remove(position); // delete from adapter array list
notifyDataSetChanged(); // refresh adapter
}
}
}
I found out how to solve my problem. I simply add this in the delete buttons on click method.
public void onClick(View v) {
DatabaseReference item=adapter.getRef(position) ;
item.removeValue();
} });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.