无法将类型为java.lang.Long的对象转换为类型(模型类)

时间:2019-04-16 12:38:24

标签: java android

我正在开发一个Android应用程序,到目前为止运行良好。我已经在welcometojungleactivity.class中设置了一个适配器,我的适配器类是plantlistadapter,即使我已经完成了本类的工作并且正在其他某个类上工作,它也可以正常工作。

welcometojungleactivity.java(包含RecyclerView)

    FirebaseRecyclerAdapter<plantsWord, plantslistAdapter> mFirebaseRecyclerAdapter = new FirebaseRecyclerAdapter<plantsWord, plantslistAdapter>(
            plantsWord.class, R.layout.item_plantslist, plantslistAdapter.class, mPlantsDatabaseReference
    ) {
        @Override
        protected void populateViewHolder(plantslistAdapter viewHolder, plantsWord model, int position) {
            viewHolder.setDetails(getApplicationContext(), model.getplantImageURL(), model.getplantLocation(), model.getplantName(), model.getplantType());
        }

        @NonNull
        @Override
        public plantslistAdapter onCreateViewHolder(ViewGroup parent, int viewType) {
            plantslistAdapter mplantslistAdapter = super.onCreateViewHolder(parent, viewType);
            mplantslistAdapter.setOnClickListener(new plantslistAdapter.ClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                }

                @Override
                public void onItemlongclick(View view, int position) {
                }
            });

            return mplantslistAdapter;
        }
    };
    mplantsreRecyclerView.setAdapter(mFirebaseRecyclerAdapter);

// plantlistAdapter.java

public class plantslistAdapter extends RecyclerView.ViewHolder {
        private View mView;
    public plantslistAdapter(View itemView) {
        super(itemView);
    }

public void setDetails(Context context, String plantImage,  String plantLocation, String plantName, String plantType){
    ImageView imgpreview = mView.findViewById(R.id.plantlist_imgpreview);
    TextView location = mView.findViewById(R.id.plantlist_location);
    TextView plantname = mView.findViewById(R.id.plantlist_plantname);
    TextView planttype = mView.findViewById(R.id.plantlist_planttype);
    Glide.with(context).load(plantImage).into(imgpreview);

    location.setText(plantLocation);
    plantname.setText(plantName);
    planttype.setText(plantType);
        }
       }

这是我的错误

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type phyla.admin.com.phyla.plantsWord
        at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
        at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:136)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:176)
        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).......

如果有人知道如何解决此问题,请告诉我,我认为此代码是正确的,因为我已检查并没有更改它。

1 个答案:

答案 0 :(得分:1)

我将不得不更多地了解这里发生的事情,但是看起来您正在尝试在某个地方使用Long类型,那里有代码将数字(可能是整数)类型转换为自定义的“单词”类型。这里使用“单词”很有趣。我经常看到在编程语言中,“单词”是一个16位无符号整数,用于存储位模式,而不是实际的数值。 Long类型可能太简单而无法在此值中存储精度。如果您确定此处的值应始终<2 ^ 31,那么我建议在使用前将其转换为int值。某些系统默认将Longs用于整数值,因此此“在您的计算机上可用”但在其他情况下可能不起作用。

同样,我不知道您正在使用的系统,所以这纯粹是猜测。