Firebase数据未在应用中显示

时间:2018-05-13 17:26:53

标签: android firebase firebase-realtime-database

我对Firebase实施稍微不熟悉,并且非常感谢我在Android应用程序中显示Firebase数据的任何帮助。 "什么数据"没有具体细节。应该显示,我只是试图将整个数据库填充到应用程序中。还没有特定的用户身份验证,为了这个应用程序,所有用户都共享数据库。

这是我的Cart.java(我想要填充的数据)

    package edu.phoenix.mbl402.week2apptt2163;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class sunglass1 extends AppCompatActivity {
    private FirebaseAuth mAuth;
    private String TAG;
    int minteger = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sunglass2);


            mAuth = FirebaseAuth.getInstance();
            Button button = (Button) findViewById(R.id.addcart);

            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {


                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                    DatabaseReference myRef = database.getReference("Octagon Glasses");

                    myRef.setValue("$64");

                    Toast.makeText(getBaseContext(), "Added to Cart", Toast.LENGTH_SHORT).show();

                }
            });


        Button button2 = (Button) findViewById(R.id.viewcart);
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                openCart();
            }
        });
    }
    public void openCart() {

        Intent intent = new Intent(this, cart.class);
        startActivity(intent);
    }
    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();


}}

这是cart.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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:showIn="@layout/activity_main"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

</RelativeLayout>

这是我的cartadapter.java:

   package edu.phoenix.mbl402.week2apptt2163;

    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    import java.util.List;

    public class cartadapter extends RecyclerView.Adapter<cartadapter.MyViewHolder> {

        private List<cartitems> cartList;

        public class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView title, price;

            public MyViewHolder(View view) {
                super(view);
                title = (TextView) view.findViewById(R.id.title);
                price = (TextView) view.findViewById(R.id.price);
            }
        }


        public cartadapter(List<cartitems> moviesList) {
            this.cartList = moviesList;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.cart_list_row, parent, false);

            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            cartitems items = cartList.get(position);
            holder.title.setText(items.getTitle());
            holder.price.setText(items.getPrice());
        }

        @Override
        public int getItemCount() {
            return cartList.size();
        }
    }

当然,还有cartitems.java:

 package edu.phoenix.mbl402.week2apptt2163;

public class cartitems {

    private String title, price;


    public cartitems() {
    }

    public cartitems(String title, String price) {
        this.title = title;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String name) {
        this.title = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String year) {
        this.price = price;
    }

}

这是cart_list_row.xml:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/row_padding_vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/row_padding_vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="@color/title"
        android:textSize="40dp"
        android:text="Your Cart"
        android:gravity="center"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="@color/title"
        android:textSize="16dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title" />

</RelativeLayout>

非常感谢您的帮助,

1 个答案:

答案 0 :(得分:0)

如果你没有使用身份验证并且没有错误(或异常),那么我认为你必须在firebase控制台数据库规则部分设置auth == null。如果你也这样做了,那么检查该类的变量名称cartitems应与firebase变量相同。