使用FirebaseRecyclerAdapter填充

时间:2017-02-27 07:18:00

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

我在使用FirebaseRecyclerAdapter

时遇到了问题

firebase实时数据库结构如下:

The structure displayed here is connected to the root of the database

我在这里要做的是, 仅从firebase实时数据库的“MainGroup”部分中的数据中获取密钥的

为此,我创建了一个名为RecyclerDataGetSetCLass的模型类 和一个名为RecyclerDataGetSetClassViewHolder

的查看者类

但我收到错误

  

com.google.firebase.database.DatabaseException:无法转换对象   类型为java.lang.String的类型   com.example.talarir.mapproject.NonAdminClasses.RecyclerDataGetSetCLass

Activity类和其他java文件如下:

1) NonAdminACtivity.java //which contains the viewholder class

public class NonAdminACtivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

public Button saveLocationBtn,getLocationBtn;
public RecyclerView nonAdminRecyclerView;

private Boolean flag=false;

private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;

private DatabaseReference mFirebaseDatabase1;
private FirebaseDatabase mFirebaseInstance1;


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


    mFirebaseInstance1 = FirebaseDatabase.getInstance();
    mFirebaseDatabase1 = mFirebaseInstance1.getReference("MainGroup");

    nonAdminRecyclerView= (RecyclerView) findViewById(R.id.recycler_view_fragment_one);
    nonAdminRecyclerView.setHasFixedSize(true);
    nonAdminRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mAuth = FirebaseAuth.getInstance();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            FirebaseUser user = firebaseAuth.getCurrentUser();

            if (user != null) {
                // User is signed in
                flag=true;
                Log.d("CooActivity", "onAuthStateChanged:signed_in:" + user.getUid());
                Toast.makeText(getApplicationContext(),"done",Toast.LENGTH_SHORT).show();

            }
            else
            {
                // User is signed out
                Log.d("CooActivity", "onAuthStateChanged:signed_out");
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                finish();

            }

        }
    };
}

@Override
public void onStart()
{
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);

    saveLocationBtn= (Button) findViewById(R.id.saveLocationOfUser);
    saveLocationBtn.setOnClickListener(this);
    getLocationBtn= (Button) findViewById(R.id.getLocationOfUser);
    getLocationBtn.setOnClickListener(this);

    FirebaseRecyclerAdapter<RecyclerDataGetSetCLass,RecyclerDataGetSetClassViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<RecyclerDataGetSetCLass, RecyclerDataGetSetClassViewHolder>(
            RecyclerDataGetSetCLass.class,
            R.layout.each_list_non_admin,
            RecyclerDataGetSetClassViewHolder.class,
            mFirebaseDatabase1
    ) {
        @Override
        protected void populateViewHolder(RecyclerDataGetSetClassViewHolder viewHolder, RecyclerDataGetSetCLass model, int position)
        {
            viewHolder.setMainGroupName(model.getMainGroupName());
        }
    };

    nonAdminRecyclerView.setAdapter(firebaseRecyclerAdapter);

}

public static class RecyclerDataGetSetClassViewHolder extends RecyclerView.ViewHolder
{
    View mView;
    public RecyclerDataGetSetClassViewHolder(View itemView) {
        super(itemView);
        mView=itemView;
    }

    public void setMainGroupName(String mainGroupName)
    {
        TextView textView= (TextView) mView.findViewById(R.id.textViewRecyclerViewNonAdmin);
        textView.setText(mainGroupName);
    }
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

@Override
protected void onDestroy() {
    FirebaseAuth.getInstance().signOut();
    super.onDestroy();
}

@Override
public void onClick(View v)
{
    if (v.getId()==R.id.saveLocationOfUser)
    {
        if (flag)
        {
            saveUserLocation_NonAdminActivity();
        }
        else
        {
            Toast.makeText(this,"firebase not ready",Toast.LENGTH_SHORT).show();
        }
    }
    if (v.getId()==R.id.getLocationOfUser)
    {
        retrieveUserLocation_NonAdminActivity();
    }
}

private void retrieveUserLocation_NonAdminActivity()
{
    CreationOfUserClass creationOfUserClass=new CreationOfUserClass(this);
    creationOfUserClass.retrieveUserDataFromFireBase(mFirebaseDatabase,mFirebaseInstance);
}

private void saveUserLocation_NonAdminActivity()
{

    FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
    final CreationOfUserClass creationOfUserClass=new CreationOfUserClass(this);
    creationOfUserClass.createUsers(user, mFirebaseDatabase,mFirebaseInstance);
}
}

2) RecyclerDataGetSetCLass //The model class

public class RecyclerDataGetSetCLass
{
private String mainGroupName;

public RecyclerDataGetSetCLass()
{
}

public RecyclerDataGetSetCLass(String mainGroupName)
{
    this.mainGroupName=mainGroupName;
}

public String getMainGroupName() {
    return mainGroupName;
}

public void setMainGroupName(String mainGroupName) {
    this.mainGroupName = mainGroupName;
}
}

3) activity_non_admin_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_non_admin_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.talarir.mapproject.NonAdminACtivity">


<Button
    android:text="SaveLocation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/saveLocationOfUser" />

<Button
    android:text="getLocation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/getLocationOfUser" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

</LinearLayout>

</LinearLayout>

4) each_list_non_admin.xml //the layout to be inflated for each recycler list element

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    xmlns:card_view="http://schemas.android.com/tools">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="2dp"
    card_view:contentPadding="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/textViewRecyclerViewNonAdmin"/>

</android.support.v7.widget.CardView>
</RelativeLayout>

错误日志如下:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.talarir.mapproject.NonAdminClasses.RecyclerDataGetSetCLass
                  at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
                  at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                  at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                  at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:151)
                  at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:140)
                  at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:183)
                  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6308)
                  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6341)
                  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5287)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5550)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5392)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5388)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2149)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1533)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1496)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:593)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3535)
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3264)
                  at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1633)
                  at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:341)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                  at android.view.Choreographer.doFrame(Choreographer.java:616)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6209)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
D/AppTracker: App Event: crash
I/Process: Sending signal. PID: 29821 SIG: 9
Application terminated.

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您要显示的每个项目的值是一个字符串(-K....)。你试图从价值中读出整个班级,而这只是不能奏效。

final FirebaseRecyclerAdapter<RecyclerDataGetSetCLass,RecyclerDataGetSetClassViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<RecyclerDataGetSetCLass, RecyclerDataGetSetClassViewHolder>(
        String.class,
        R.layout.each_list_non_admin,
        RecyclerDataGetSetClassViewHolder.class,
        mFirebaseDatabase1
) {
    @Override
    protected void populateViewHolder(RecyclerDataGetSetClassViewHolder viewHolder, String model, int position)
    {
        String key = adapter.getRef(position).getKey();
        viewHolder.setMainGroupName(key);
    }
};

在此代码段中,我会查找所显示项目的关键字,这可能是您正在寻找的价值。