使用适用于Android Studio的Firebase更新特定值

时间:2017-12-29 19:38:59

标签: android firebase firebase-realtime-database

如何更新Android Studio中的值? 我想要更新“msg_viwe_date”,我将其置为空,

我尝试了很多在谷歌搜索中找到的方法。我的问题是有一个像“-L1PC7-zenLfcvx36cG0”这样的seq,所以我无法更新我想要的值。 json方法如下图所示。

json

如何更新Android Studio中的值? 我想要更新“msg_viwe_date”,我将其置为空,

我尝试了很多在谷歌搜索中找到的方法。我的问题是有一个像“-L1PC7-zenLfcvx36cG0”这样的seq,所以我无法更新我想要的值。 json方法在此图像中看起来像。     包com.androidchatapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.MutableData;
import com.firebase.client.Transaction;
import com.firebase.client.ValueEventListener;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Chat extends AppCompatActivity {
    private DatabaseReference mDatabase;

    LinearLayout layout;
    RelativeLayout layout_2;
    ImageView sendButton;
    EditText messageArea;
    ScrollView scrollView;
    Firebase reference1, reference2;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String currentDateandTime = sdf.format(new Date());
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        mDatabase = FirebaseDatabase.getInstance().getReference();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        layout = (LinearLayout) findViewById(R.id.layout1);
        layout_2 = (RelativeLayout)findViewById(R.id.layout2);
        sendButton = (ImageView)findViewById(R.id.sendButton);
        messageArea = (EditText)findViewById(R.id.messageArea);
        scrollView = (ScrollView)findViewById(R.id.scrollView);

        Firebase.setAndroidContext(this);
        //reference1 = new Firebase("https://androidchatapp-76776.firebaseio.com/messages/" + UserDetails.username + "_" + UserDetails.chatWith);
        reference1 = new Firebase("https://leqaafm-c0d5a.firebaseio.com/messages/" + UserDetails.username + "_" + UserDetails.chatWith);
        //reference2 = new Firebase("https://androidchatapp-76776.firebaseio.com/messages/" + UserDetails.chatWith + "_" + UserDetails.username);
        reference2 = new Firebase("https://leqaafm-c0d5a.firebaseio.com/messages/" + UserDetails.chatWith + "_" + UserDetails.username);

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String messageText = messageArea.getText().toString();

                if(!messageText.equals("")){
                    Map<String, String> map = new HashMap<String, String>();

                    map.put("message", messageText);
                    map.put("user", UserDetails.username);
                    map.put("msg_date",currentDateandTime);
                    map.put("msg_viwe_date"," ");

                    reference1.push().setValue(map);
                    reference2.push().setValue(map);

                    messageArea.setText("");
                }
            }
        });

        reference1.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Map map = dataSnapshot.getValue(Map.class);
                String msg_date = map.get("msg_date").toString();
                String message = map.get("message").toString();
                String userName = map.get("user").toString();

                if(userName.equals(UserDetails.username)){
                    addMessageBox("You:-\n"+msg_date +"-\n"+ message, 1);
                }
                else{
                    addMessageBox(UserDetails.chatWith + ":-\n" +msg_date +"-\n"+ message, 2);

                }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                //String clubkey = dataSnapshot.getKey();
               //Toast.makeText(Chat.this, clubkey, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }

    public void addMessageBox(String message, int type){
        TextView textView = new TextView(Chat.this);
        textView.setText(message);

        LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp2.weight = 1.0f;

        if(type == 1) {
            lp2.gravity = Gravity.LEFT;
            textView.setBackgroundResource(R.drawable.bubble_in);
        }
        else{
            lp2.gravity = Gravity.RIGHT;
            textView.setBackgroundResource(R.drawable.bubble_out);
        }
        textView.setLayoutParams(lp2);
        layout.addView(textView);
        scrollView.fullScroll(View.FOCUS_DOWN);
    }
}

2 个答案:

答案 0 :(得分:0)

要更新值,您可以直接在引用上使用setValue()方法,如下所示:

reference1.child("msg_viwe_date").child("-L1PC7-zenLfcvx36cG0").setValue("yourNewValue");

要获得该随机密钥,当您推动获取它时,请将其存储到如下变量中:

String key = reference1.child("msg_viwe_date").push().getKey();

然后更新:

reference1.child("msg_viwe_date").child(key).setValue("yourNewValue");

答案 1 :(得分:0)

我了解您的困惑: 要更新单个值,请使用简单的通用语句:

FirebaseDatabase.getInstance().getReference().child("node_id").child("message_view_date").setValue(data);

幸福编码:)