为什么我的数据没有显示在recyclerview中?

时间:2019-05-19 20:51:54

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

我一直在试图弄清为什么没有数据传输到RecyclerWiew。到目前为止,仍然没有运气。在某些情况下,我什至得到:

  

“ com.google.firebase.database.DatabaseException:无法将类型为java.lang.String的对象转换为类型”错误

MainActivity

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private RecyclerView recyclerView;
    private DatabaseReference databaseReference;
    private FirebaseRecyclerOptions<Pond> options;
    private FirebaseRecyclerAdapter<Pond, PondViewHolder> adapter;

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

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);


        databaseReference = FirebaseDatabase.getInstance().getReference().child("pi1DetailTest");

        options = new FirebaseRecyclerOptions.Builder<Pond>().setQuery(databaseReference, Pond.class).build();

        adapter = new FirebaseRecyclerAdapter<Pond, PondViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull PondViewHolder holder, int position, @NonNull Pond model) {
                holder.txtViewPiID.setText(model.getPiId());
                holder.txtViewPondName.setText(model.getPondName());
                holder.txtViewPondLocation.setText(model.getLocation());
            }

            @NonNull
            @Override
            public PondViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_main_cardview, viewGroup, false);
                return new PondViewHolder(view);
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

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

ViewHolder

public class PondViewHolder extends RecyclerView.ViewHolder {
    public TextView txtViewPiID;
    public TextView txtViewPondName;
    public TextView txtViewPondLocation;

    public PondViewHolder(@NonNull View itemView) {
        super(itemView);
        txtViewPiID = (TextView) itemView.findViewById(R.id.txtViewPiID);
        txtViewPondName = (TextView) itemView.findViewById(R.id.txtViewPondName);
        txtViewPondLocation = (TextView) itemView.findViewById(R.id.txtViewPondLocation);
    }
}

型号

public class Pond {
    public String piId;
    public String pondName;
    public String location;

    public Pond(String piId, String pondName, String location) {
        this.piId = piId;
        this.pondName = pondName;
        this.location = location;
    }

    public Pond(){

    }

    public String getPiId() {
        return piId;
    }

    public void setPiId(String piId) {
        this.piId = piId;
    }

    public String getPondName() {
        return pondName;
    }

    public void setPondName(String pondName) {
        this.pondName = pondName;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

我希望包含Pond类数据的cardview将显示在recyclerview中。请帮忙。谢谢。

其他信息==这是logcat的错误

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.testappagain.Pond
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@17.0.0:418)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@17.0.0:214)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@17.0.0:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@17.0.0:212)
        at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
        at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
        at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
        at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
        at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
        at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)
        at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:407)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:603)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1Datastructure: Where I want to get the data from.

2Entire Data Structure

2 个答案:

答案 0 :(得分:0)

  1. 尝试分开适配器和视图。从RecyclerView扩展适配器。 ViewHolder放入适配器类
  2. 然后从db创建一个BYffer数据列表并将其提供给适配器

您能显示出全部错误吗?

答案 1 :(得分:0)

根据您的最新评论,要仅在RecyclerViewSomeplacepi 1这三个值中显示pondName,您需要更改获取方式数据库中的数据。因此,您将收到以下错误:

  

com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.testappagain.Pond类型

由于在pi1DetailTest节点下存在类型为String的对象,而不是类型为Pond的对象,并且Java中无法转换类型的对象字符串到池塘。因此,在您的情况下,无需使用FirebaseRecyclerOptionsFirebaseRecyclerAdapter,只需使用ArrayAdapterListView就像这样:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference pi1DetailTestRef = rootRef.child("pi1DetailTest");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String location = ds.child("location").getValue(String.class);
            list.add(location);
            String piId = ds.child("piId").getValue(String.class);
            list.add(piId);
            String pondName = ds.child("pondName").getValue(String.class);
            list.add(pondName);
        }
        ListView listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
pi1DetailTestRef.addListenerForSingleValueEvent(eventListener);