猿问

如何使用按钮从 firebase 列表适配器和 firebase 中删除项目

我有一个从 Firebase 数据库读取数据并在 Firebase 列表适配器中显示数据的活动。列表视图中的每个项目都有 2 个编辑文本和一个用于从列表视图和 Firebase 数据库中删除项目的按钮。下面是显示 firebase 列表适配器的代码


      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) {.....

列表视图工作正常。数据是从 firebase 加载的,但我希望能够使用此按钮删除来从列表视图和 firebase 数据库中删除项目。我做了研究,只找到了点击项目而不使用按钮的方法,一个例子是这个Android studio 从 listview 和 firebase 中删除项目 ,但我不知道如何将原理应用到与数据库中的项目关联的按钮。请提供协助

下面是我的适配器类的代码 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;

     }


  } 


慕容森
浏览 106回答 2
2回答

梵蒂冈之花

您可以在适配器内执行此操作// make this function inside your adapterprotected void populateView(View v, ArrayList<TravelDetails> models, final int position) {&nbsp; &nbsp; Button delete=(Button)v.findViewById(R.id.button_accept);&nbsp; &nbsp; final TextView z=(TextView)v.findViewById(R.id.dropoff);&nbsp; &nbsp; final TextView text=(TextView)v.findViewById(R.id.txref);&nbsp; &nbsp; text.setText(model.getTxt_Ref());&nbsp; &nbsp; z.setText(model.getDropoffspot());&nbsp; &nbsp; delete.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // code to delete from firebase&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; models.remove(position); // delete from adapter array list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notifyDataSetChanged(); // refresh adapter&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

MM们

我找到了如何解决我的问题。我只是将其添加到单击方法的删除按钮中。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;onClick(View&nbsp;v)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DatabaseReference&nbsp;item=adapter.getRef(position)&nbsp;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.removeValue();} }) ;
随时随地看视频慕课网APP

相关分类

Java
我要回答