如何在Firebase(Android)中添加和查看子对象? [英] How to add and view child objects in Firebase (Android)?

查看:127
本文介绍了如何在Firebase(Android)中添加和查看子对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的问题,其中我有我的文件如下,我想能够查看所有子项目,并添加新的子项目。

据我所知,您将不得不修改Java文件中的代码,但是我无法理解如何操纵我自己的代码,我试图去理解令人困惑的文档由Firebase提供。

下面是我的数据库结构,后面跟着我的代码。任何帮助将不胜感激。

我现在唯一的问题是如何正确填充与我的数据库的以下结构的值列表视图。

如下:Messages - > Message - >其中有连接字符串的值

I希望这不是混淆

这是我的MainActivity.java

  package com.example.sean.messengerappsean; 

导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.view.View;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.TextView;

导入com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
import com.firebase.client.collection.ArraySortedMap;
import com.firebase.client.realtime.util.StringListReader;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
// TextView mTextFieldCondition;
EditText editUsername,editValue;
ArrayAdapter arrayAdapter;
按钮btnSend;
Firebase mRef;


@Override
protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


@Override
protected void onStart(){
super.onStart();
// mTextFieldCondition =(TextView)findViewById(R.id.textViewCondition);
editUsername =(EditText)findViewById(R.id.editUsername);
editValue =(EditText)findViewById(R.id.editValue);
btnSend =(Button)findViewById(R.id.btnSend);
mRef = new Firebase(https://androidmessagetest.firebaseio.com/); //可以使用这里的代码发布到特定的位置或DB
//尝试设置列表视图
最终ListView listView =(ListView)findViewById(R.id.listview);
final ArrayList< String> msgsArray = new ArrayList< String>(); //使用此方法来查看视图





$ b final ValueEventListener valueEventListener = mRef.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){



// ---------- CONDITION FOR LISTING DATA- ---------------
//可能需要将数值放入数组中,并按照发布的顺序读取它们
// String text = dataSnapshot.child( (输入用户名3)。getValue(String.class);
//mTextFieldCondition.setText(text);

// POPULATE LISTVIEW
for DataSnapshot Datasnapshot:dataSnapshot.getChildren()){
msgsArray.add(Datasnapshot.child(Messages)。child(Message).getKey());

}

ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,andro id.R.layout.simple_list_item_1,msgsArray);
listView.setAdapter(arrayAdapter);





$覆盖
public void onCancelled(FirebaseError firebaseError){

}
});

btnSend.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//需要动态地将值更改为USERNAME :MESSAGE
String textoutput = String.valueOf(editUsername.getText());
String MessageText = String.valueOf(editValue.getText());

//固定消息添加孩子以及如何给消息添加价值
//尝试使用下面的代码来获取消息后面的消息

mRef.child(Messages)。child(Message)。push ).setValue(textoutput +:+ MessageText);


$ b}
});

//增加查找数据功能



$ b code $ <$ $ $ p>

这是我的XML文件activity_main.xml:

 <?xml version =1.0encoding =utf-8?> 

< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com / tools
android:id =@ + id / activity_main
android:layout_width =match_parent
android:layout_height =match_parent$ b $ android:paddingBottom =@
android:paddingLeft =@ dimen / activity_vertical_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_margin
android:paddingTop =@ dimen / activity_vertical_margin
工具:上下文= com.example.sean.messengerappsean.MainActivity >


< LinearLayout
android:layout_width =match_parent
android:layout_height =wrap_content>

<! - TextView
android:id =@ + id / textViewCondition
android:layout_width =wrap_content
android:layout_height = wrap_content
android:layout_above =@ + id / mTextFieldCondition
android:layout_centerHorizo​​ntal =true
android:layout_marginBottom =89dp
android:text =Condition /> - >

//输入以下代码查看数据输出
android:id =@ + id / listview
android:layout_width = match_parent
android:layout_height =wrap_content
>< / ListView>

< / LinearLayout>


解决方案

这些导入适用于旧SDK中的类: / p>

  import com.firebase.client.DataSnapshot; 
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;

以下是新的SDK:

  import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase;

您不能混用这两个SDK。您应该只使用新的SDK。从build.gradle依赖关系中删除这条语句:
$ b $ pre $编译com.firebase:firebase-client-android:2.x。 x'

,并进行适当的代码更改以使用新的SDK。文档和示例是此处。还有一个升级指南


I am having a similar issue in which I have my files as follow in which I want to be able to view all child items and add new child items.

I understand that you will have to modify the code in the Java file, but I am having trouble understanding how to manipulate my own code and I tried to follow the confusing documentation provided by Firebase.

Below is the structure of my database followed by my code. Any help would be greatly appreciated.

The only problem I have now is how to properly populate the listview with the values of the following structure of my database.

It is as follows: Messages --> Message --> value of which has concatenated string

I hope this isn't confusing

Here is my MainActivity.java

     package com.example.sean.messengerappsean;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
import com.firebase.client.collection.ArraySortedMap;
import com.firebase.client.realtime.util.StringListReader;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //TextView mTextFieldCondition;
    EditText editUsername, editValue;
    ArrayAdapter arrayAdapter;
    Button btnSend;
    Firebase mRef;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //mTextFieldCondition = (TextView) findViewById(R.id.textViewCondition);
        editUsername = (EditText) findViewById(R.id.editUsername);
        editValue = (EditText) findViewById(R.id.editValue);
        btnSend = (Button) findViewById(R.id.btnSend);
        mRef = new Firebase("https://androidmessagetest.firebaseio.com/"); //CAN USE THE CODE HERE TO PUBLISH TO SPECIFIC LOCATION OR DB
        //TRYING TO SET LISTVIEW
        final ListView listView = (ListView) findViewById(R.id.listview);
        final ArrayList<String> msgsArray = new ArrayList<String>(); //USE THIS FOR POPULATING THE VIEW






        final ValueEventListener valueEventListener = mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {



                //----------CONDITION FOR LISTING DATA----------------
                //MIGHT NEED TO PUT VALUES INTO ARRAY AND READ THEM OUT IN ORDER OF PUBLISH
                //String text = dataSnapshot.child("Message").child("Enter Username3").getValue(String.class);
                //mTextFieldCondition.setText(text);

                //POPULATE LISTVIEW
                for (DataSnapshot Datasnapshot : dataSnapshot.getChildren()) {
                   msgsArray.add(Datasnapshot.child("Messages").child("Message").getKey());

                }

                ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, msgsArray);
                listView.setAdapter(arrayAdapter);




            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //NEED TO DYNAMICALLY CHANGE VALUE TO USERNAME: MESSAGE
                String textoutput = String.valueOf(editUsername.getText());
                String MessageText = String.valueOf(editValue.getText());

                //FIXED MESSAGE ADD CHILD AND HOW TO ADD VALUE TO MESSAGE
                //TRY TO USE BELOW CODE FOR MESSAGES LATER ON

                mRef.child("Messages").child("Message").push().setValue(textoutput + ": " + MessageText);



            }
        });

        //ADD FUNCTION TO LOOK FOR DATA


    }
}

Here is my XML file activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.sean.messengerappsean.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--<TextView
            android:id="@+id/textViewCondition"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/mTextFieldCondition"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="89dp"
            android:text="Condition" />-->

        //ENTER IN CODE BELOW FOR VIEWING OUTPUT OF DATA
        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ></ListView>

    </LinearLayout>

    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:text="Send" />

    <EditText
        android:id="@+id/editValue"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/btnSend"
        android:layout_toLeftOf="@+id/btnSend"
        android:layout_toStartOf="@+id/btnSend"
        android:backgroundTint="@color/colorPrimaryDark"
        android:inputType="textMultiLine"
        android:text="Enter Message Here" />

    <EditText
        android:id="@+id/editUsername"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_above="@+id/btnSend"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_gravity="bottom"
        android:background="@color/colorAccent"
        android:inputType="textCapWords"
        android:text="Enter Username" />
</RelativeLayout>

解决方案

These imports are for classes in the legacy SDK:

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;

These are for the new SDK:

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

You cannot mix the two SDKs. You should use the new SDK only. Remove this statement from your build.gradle dependencies:

compile 'com.firebase:firebase-client-android:2.x.x'

and make the approriate code changes to use the new SDK. Documentation and examples are here. There is also an Upgrade Guide.

这篇关于如何在Firebase(Android)中添加和查看子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆