简体   繁体   中英

Can't convert object of type java.lang.String [Android]

在此处输入图像描述

when I try to retrieve the child path the application closes, I just want to be able to set the value of the child path in the texview but the app always closes and in the logcat it tells me: Can't convert object of type java.lang.String to type com.musicapp.owls.ClassesFolder.Recover.

Apparently my question has already been created on the platform but really the answers I see do not help me, I hope you can help me, I will mark the answer as answered if you help me

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setReverseLayout(true);
        mLayoutManager.setStackFromEnd(true);
        recyclerView_album_songs.setLayoutManager(mLayoutManager);


      mDatabase = FirebaseDatabase.getInstance().getReference("Artistas/"+artist_name+"/Albumes/"+album_name);


      FirebaseRecyclerOptions<Recover> options =
                new FirebaseRecyclerOptions.Builder<Recover>()
                        .setQuery(mDatabase,Recover.class)
                        .build();



      adapter = new FirebaseRecyclerAdapter<Recover, RecyclerView.ViewHolder>(options) {
          @Override
          protected void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull Recover model) {

              TextView textView = holder.itemView.findViewById(R.id.song_name_txt);
              textView_album.setText(model.getName());

              textView.setText(model.getPath());    

          }

          @NonNull
          @Override
          public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
              View view = LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.list_songs_inflated, parent, false);

              return new RecyclerView.ViewHolder(view) {
              };

          }


      };
  
        adapter.startListening();
        recyclerView_album_songs.setAdapter(adapter);


    }
}

Recover constructor

public class Recover {


    public String Nombre;
    public String ImageView;
    public String ImageAlbum;
    public String NameAlbum;
    public String path;



    // Default constructor required for calls to
    // DataSnapshot.getValue(User.class)
    public Recover() {


    }


    public Recover(String Nombre, String ImageView, String imageView, String nameAlbum, String path) {
        this.Nombre = Nombre;
        this.ImageView= ImageView;
        this.ImageAlbum = imageView;
        this.NameAlbum = nameAlbum;
        this.path = path;

    }



    public String getArtist() {
        return Nombre;
    }

    public String getUrl() {
        return ImageView;
    }

    public String getImage() {
        return ImageAlbum;
    }

    public String getName() {
        return NameAlbum;
    }

    public String getPath() {
        return path;
    }

}

Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.musicapp.buhos, PID: 30375
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.musicapp.buhos.ClassesFolder.Recover
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
        at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
        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:36)
        at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:109)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
        at android.view.View.layout(View.java:23033)
        at android.view.ViewGroup.layout(ViewGroup.java:6468)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:23033)
        at android.view.ViewGroup.layout(ViewGroup.java:6468)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:23033)
        at android.view.ViewGroup.layout(ViewGroup.java:6468)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
        at android.view.View.layout(View.java:23033)
        at android.view.ViewGroup.layout(ViewGroup.java:6468)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:23033)
        at android.view.ViewGroup.layout(ViewGroup.java:6468)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:23033)
        at android.view.ViewGroup.layout(ViewGroup.java:6468)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
E/AndroidRuntime:     at com.android.internal.policy.DecorView.onLayout(DecorView.java:804)
        at android.view.View.layout(View.java:23033)
        at android.view.ViewGroup.layout(ViewGroup.java:6468)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3599)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3067)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2070)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8400)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1058)
        at android.view.Choreographer.doCallbacks(Choreographer.java:880)
        at android.view.Choreographer.doFrame(Choreographer.java:813)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1043)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8037)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

The FirebaseRecyclerAdapter from FirebaseUI assumes that you want to show a list of child nodes from the database. So the query you pass to setQuery needs to be either query that selects one or more albums, or the entire list of albums you want to show.

You're loading a single album here:

mDatabase = FirebaseDatabase.getInstance().getReference("Artistas/"+artist_name+"/Albumes/"+album_name);

So when you pass that to the FirebaseRecyclerAdapter , it tries to display each child property of that album in a view holder in the adapter. And that's not what you want.

If you only want to show a single album, a RecyclerView is typically not the best widget to use. But if you do really want to use that, you'll have to create a query to select that one album:

Query query = FirebaseDatabase.getInstance() 
    .getReference("Artistas/"+artist_name+"/Albumes")
    .orderByKey()
    .equaTo(album_name);

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

So now you can pass the query to Firebase:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM