Firebase Recycler视图在向下滚动时崩溃

时间:2018-11-21 15:08:08

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

Hii我正在尝试从Firebase数据库和存储中读取数据到Firebase Recycler,一切正常,直到向下滚动Recycler视图,然后应用程序崩溃了,这是我的代码

模型类

 package com.example.ace.park;

public class model {
    public String name,num,type,sno;


    public model(String name, String num, String type, String sno) {
        this.name = name;
        this.num = num;
        this.type = type;
        this.sno = sno;
    }

    public model() {
    }
}

这是JAVA CLASS

package com.example.ace.park;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class dis extends AppCompatActivity {


    NavigationView navigationView;
    TextView textView;
    Intent intent;
    FirebaseRecyclerAdapter<model, displayAdapter> adapter;
    FirebaseRecyclerOptions<model> options;
    RecyclerView recyclerView;
    DatabaseReference mDatabase,query;

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

        recyclerView = findViewById(R.id.myRecView);


        mDatabase = FirebaseDatabase.getInstance().getReference().child("parkings");
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        query = FirebaseDatabase.getInstance().getReference().child("parkings");

        options = new FirebaseRecyclerOptions.Builder<model>()
                .setQuery(query, model.class)
                .build();

        adapter = new FirebaseRecyclerAdapter<model, displayAdapter>(
                options) {
            @Override
            public displayAdapter onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler, parent, false);


                return new displayAdapter(v);
            }

            @Override
            protected void onBindViewHolder(displayAdapter holder, final int position, final model current) {

                holder.setName(current.name);
                holder.setTotal(current.num);
                holder.setType(current.type);
                holder.setImage(getApplicationContext(),current.sno,current.sno);

                holder.mview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(dis.this, ""+position, Toast.LENGTH_SHORT).show();


                    }
                });
            }


        };

        //Populate Item into Adapter
        recyclerView.setAdapter(adapter);



    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();



    }

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

}

这里是适配器

package com.example.ace.park;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

import java.io.File;
import java.io.IOException;

public class displayAdapter extends RecyclerView.ViewHolder {

    public View mview;
    public displayAdapter(View itemView) {
        super(itemView);
        mview = itemView;

    }
    public void setName(String name){
        TextView recName = mview.findViewById(R.id.recyclerName);
        recName.setText(name);
    }
    public void setTotal(String title){
        TextView t = mview.findViewById(R.id.recyclerTotal);
        t.setText(title);
    }
    public void setType(String type){
        TextView t = mview.findViewById(R.id.recyclerType);
        t.setText(type);
    }
    public void setImage(Context ctx, String image,String user){
        final ImageView iv = mview.findViewById(R.id.recyclerImg);

        FirebaseStorage storage = FirebaseStorage.getInstance();
        StorageReference storageReference = storage.getReference().child("Parkings"+"/"+image);
        try {
            final File localFile = File.createTempFile("images", "jpg");
            storageReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                    Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
                    iv.setImageBitmap(bitmap);

                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                }
            });
        } catch (IOException e ) {
            Toast.makeText(ctx, "Error in Adapter", Toast.LENGTH_SHORT).show();



        }

    }

}

这是回收站的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    app:cardCornerRadius="10dp"
    android:layout_height="wrap_content">

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

        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerName"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:textColor="#212020"
            android:textSize="30sp"
            android:text="@string/name_of_place"
            />

        <ImageView
            android:layout_width="300dp"
            android:layout_marginTop="30dp"
            android:layout_height="230dp"
            android:scaleType="centerCrop"
            android:paddingLeft="10dp"
            android:layout_gravity="center"
            android:paddingRight="10dp"
            android:id="@+id/recyclerImg"
            android:contentDescription="@string/img"
            />


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:textColor="#615f5f"
            android:textSize="18sp"
            android:id="@+id/recyclerTotal"
            android:text="@string/tsp"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_marginTop="5dp"
            android:text="@string/type"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:id="@+id/recyclerType"
            android:layout_height="25dp"
            android:textSize="18sp"
            android:layout_marginBottom="5dp"
            />
    </LinearLayout>


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

Logcat给出了错误

11-21 20:37:32.885 9681-9681 / com.example.ace.park E / Android运行时:致命异常:主要     流程:com.example.ace.park,PID:9681     com.google.firebase.database.DatabaseException:无法将java.lang.Long类型的对象转换为com.example.ace.park.model类型         在com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database @@ 16.0.5:423)         com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database @@ 16.0.5:214)         在com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database @@ 16.0.5:79)         com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database @@ 16.0.5:212)         在com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)         在com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)         在com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)         在com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)         在com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)         在com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)         在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6673)         在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6714)         在android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)         在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)         在android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:285)         在android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:342)         在android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:358)         在android.support.v7.widget.GapWorker.prefetch(GapWorker.java:365)         在android.support.v7.widget.GapWorker.run(GapWorker.java:396)         在android.os.Handler.handleCallback(Handler.java:789)         在android.os.Handler.dispatchMessage(Handler.java:98)         在android.os.Looper.loop(Looper.java:164)         在android.app.ActivityThread.main(ActivityThread.java:6541)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

this is image of database structure

0 个答案:

没有答案