2017-08-20 36 views
0

我有一个“发送”按钮内的dialogFragment按钮,onClick事件将新数据按键值推送到firebase。两个不同动作的一个按钮

我希望这个按钮在用户点击特定按钮时也像是“更新”按钮。数据将在与之前相同的关键值下在Firebase中进行更新。

这是发送按钮的onClick方法:

send_btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

        //some code .....// 

        DatabaseReference newPost = mDatabase.push(); 
        str_key = newPost.getKey(); 
        trempData = new TrempData(str_key, str_uid, str_name, str_phone, str_from, str_to, str_date, str_time, str_extra, str_timestamp); 
        newPost.setValue(trempData); 
        Toast.makeText(getActivity(), "Tremp Added", Toast.LENGTH_SHORT).show(); 
        dismiss(); 
       } 


     }); 

有什么建议?

+0

当他打开开始状态 –

+0

我不知道什么样的对话我undrestand你的问题.. 开始状态是当用户点击发送btn新数据推送到firebase – Nauruto

+0

后它推动你想要的按钮升级状态 –

回答

0

这是一个例子:

private final int BUTTON_UPDATE = 1; 
private final int BUTTON_SEND = 2; 
private int buttonStatus = 0; 

send_btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      switch(buttonStatus) 
      { 
      case BUTTON_UPDATE: { // your update code here} 
      case BUTTON_SEND : { // your send code here 
     } 

buttonStatus会控制一下操作按钮就行了。

0
  1. 将一个布尔值在sharedPrefrences并保持假最初时
    用户将数据发送到火力更新布尔为true。

  2. 中的onClick检查值是true或者利用虚假的,如果别人和 执行代码相应

0

比实施更重要的是,你认为这是可以做到的方式。因此,这些情况下的基本方法是使用布尔值变量。

为什么?因为它可以用来指示按钮是否处于特定状态。 所以,你可以做这样的事情。

boolean b=false; 
//set your button in the initial state you want(submit in your case) 
//In onClick() method 
if(!b){   //button in submit state 
b=true;   
//do submit stuff 
send_btn.setText("update"); 
} 
else{   //button in update state 
b=false; 
//do update stuff 
send_btn.setText("submit"); 
} 

在这种情况下,b的真实值表示按钮处于“更新”状态。

0

您可以使用视图的标签,这和更新状态,你需要

Button button = findViewById(R.id.btn); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (button.getTag().equals("send")) { 
        // push the value to firebase 

        // set the tag to update and the text 
        button.setTag("update"); 
        button.setText("update"); 
       }else if (button.getTag().equals("update")){ 
        // update the value in firebase 
       } 
      } 
     }); 

XML

<Button 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:tag="send" 
     android:text="send" /> 
+0

我希望它被更改为“更新”,只有当我点击位于列表视图(在另一个活动中)的特定按钮。但是当我尝试更改标记应用程序关闭 – Nauruto

+0

此代码中的答案如果有在同一活动中,如果在另一个活动中,我认为你应该使用prefrence来存储值 –

相关问题