从Firebase获取数据的Android片段未显示

时间:2019-02-28 15:03:23

标签: java android firebase fragment

我有一个带有底部导航视图的应用程序,该视图在4个片段之间切换,我正在尝试使用FirebaseRecyclerView Adapter从Firebase的数据中显示这些片段。

我已经完成所有设置,但是没有显示FirebaseRecyclerView Adapter膨胀的布局。

我的 MainAcitivity.java

package com.pap.diogo.pilltrack;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.firebase.ui.database.SnapshotParser;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment = null;

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    selectedFragment = new HomeFragment();
                    break;
                case R.id.navigation_pills:
                    selectedFragment = new PillsFragment();
                    break;
                case R.id.navigation_appointment:
                    selectedFragment = new AppointsFragment();
                    break;
                case R.id.navigation_account:
                    selectedFragment = new AccountFragment();
                    break;
            }
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();

            return true;
        }
    };

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

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user == null) {
            Intent VerifyLogin = new Intent(MainActivity.this, Launcher.class);
            VerifyLogin.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(VerifyLogin);
        }

        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
};

我的 AccountFragment.java

package com.pap.diogo.pilltrack;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.firebase.ui.database.SnapshotParser;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

public class AccountFragment extends Fragment {
    private RecyclerView AccountUsers;
    private View mMainView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mMainView = inflater.inflate(R.layout.fragment_account, container, false);

        AccountUsers = mMainView.findViewById(R.id.accountlist);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        AccountUsers.setLayoutManager(linearLayoutManager);
        AccountUsers.setHasFixedSize(true);

        return mMainView;
    }

    @Override
    public void onStart() {
        super.onStart();

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        final String userid = user.getUid();

        final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");

        FirebaseRecyclerOptions<Account> AccountQ = new FirebaseRecyclerOptions.Builder<Account>().setQuery(ref, Account.class).setLifecycleOwner(this).build();

        FirebaseRecyclerAdapter<Account, AccountInfo> AccountAdapter = new FirebaseRecyclerAdapter<Account, AccountInfo>(AccountQ){

            @NonNull
            @Override
            public AccountInfo onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                return new AccountInfo(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.account, viewGroup, false));
            }

            @Override
            protected void onBindViewHolder(@NonNull final AccountInfo holder, int position, @NonNull final Account model) {
                ref.child(userid).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        final String name = dataSnapshot.child("name").getValue().toString();
                        final String age = dataSnapshot.child("idade").getValue().toString();
                        holder.setName(name);
                        holder.setAge(age);
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }


        };
        AccountUsers.setAdapter(AccountAdapter);
    }

    public static class AccountInfo extends RecyclerView.ViewHolder{
        View AccountL;

        public AccountInfo(@NonNull View itemView) {
            super(itemView);

            AccountL = itemView;
        }

        public void setName(String name){
            TextView AccountName = AccountL.findViewById(R.id.AccountName0);
            AccountName.setText(name);
        }

        public void setAge(String age){
            TextView AccountAge = AccountL.findViewById(R.id.AccountAge0);
            AccountAge.setText(age);
        }
    }
}

我的 account.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="match_parent">

    <RelativeLayout
        android:id="@+id/AccountUser"
        android:layout_width="match_parent"
        android:layout_height="163dp"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp"
        android:background="@drawable/edit_bg"
        android:padding="15dp">

        <RelativeLayout
            android:id="@+id/AccountImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_user"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/AccountInfos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/AccountImage"
            android:layout_toEndOf="@id/AccountImage"
            android:layout_marginStart="15dp"
            android:layout_marginLeft="15dp">

            <TextView
                android:id="@+id/AccountName0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:text="Nome1"
                android:textColor="@color/colorWhite"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/AccountAge0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/AccountName0"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:text="Idade1"
                android:textColor="@color/colorWhite"
                android:textSize="20sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/AccountChangePass"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/AccountAge0"
                android:text="Mudar Palavra-Passe"
                android:textColor="@color/colorWhite"
                android:textSize="18sp"
                android:textStyle="bold"
                android:textAllCaps="false"
                android:padding="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/custom_button"/>
        </RelativeLayout>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/AccountUser">

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginEnd="15dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/ic_add"
            android:padding="5dp"
            android:background="@drawable/add_button"/>
    </RelativeLayout>

</RelativeLayout>

我的 fragment_account.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="match_parent">

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

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

templates

1 个答案:

答案 0 :(得分:0)

我修复了。

我要做的就是删除以下代码行:

AccountUsers.setHasFixedSize(true);