DatabaseException: Can not convert object of type java.lang.String to type

1

I'm giving this bug in a recyclerview and I do not know how to fix it, I'm trying to show the events that I have ... I use another recyclerView in the app and it works without problems

public class User {

    private String name;
    private String email;
    private String avatar;
    private List<String> events;
    private String vote;


    //Constructor
    public User() {
        this.events = new ArrayList<>();
    }

    public User(List<String> events) {
        this.events = events;
    }

    public User(String name, String email, String avatar) {

        this.name = name;
        this.email = email;
        this.avatar = avatar;
        this.events = new ArrayList<>();
    }

    public User(String name, String email, String avatar, List<String> events) {

        this.name = name;
        this.email = email;
        this.avatar = avatar;
        this.events = events;

    }

public class GalleryFragment extends Fragment {

RecyclerView recyclerView;
List<String> eventos;
AdapterGallery adapter;



public GalleryFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View vista=inflater.inflate( R.layout.fragment_gallery, container, false );

    FirebaseDatabase database=FirebaseDatabase.getInstance();
    eventos = new ArrayList<>();


    recyclerView = vista.findViewById( R.id.rv_gallery );
    adapter=new AdapterGallery( eventos );
    recyclerView.setAdapter( adapter );

    FirebaseUser userAut;
    userAut = FirebaseAuth.getInstance().getCurrentUser();

    database.getReference("Users").child(userAut.getUid()).addListenerForSingleValueEvent( new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


                // String lista=snapshot.getValue(String.class);
                User lista=dataSnapshot.getValue(User.class);
                //RecyclerList lista=snapshot.getValue(RecyclerList.class);
                eventos=lista.getevents() ;// en debug lo recibe bien

            adapter.notifyDataSetChanged();
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    } );


    return vista;
}

}

    
asked by lujan 12.05.2018 в 01:34
source

1 answer

0

The problem for the one that causes you:

  

DatabaseException: Can not convert object of type java.lang.String to   type

is because you are simply declaring the List list without specifying the type you will get:

You do:

List eventos;

Instead of:

List<User> eventos;

And since the indicator new ArrayList<>(); is abtract, it does not take the type you want if you do not specify it in the generic type of List.

Now, if you want to populate your adapter with events, if you look at the Firebase tree, that node contains children String , it is not enough to have a List<User> , since User contains all properties even if you only assign the value to events.

You have two options:

  • Recommended if your adapter is only managed to show "events": you must declare the list that manages the adapter as List<String> and instead pass the data as eventos.add(user); , pass it like this:

eventos.addAll(user.getEvents());

You must eliminate the for cycle, since you only have one node that manages the list.

  • Less recommended : if you are going to use a list where you only need the user's events field. Leave everything like this and in the adapter, you get the item you're going to show, like this:

items.getEvents().get(position);

Assuming that items is type List<User> And in getItemCount you should return:

items.events.size();

The problem with the latter is that you will have a User object with fields that you will not use.

In the end you should have something like this using the recommended option:

 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
    eventos.clear();

    User user = dataSnapshot.getValue(User.class);
    eventos.addAll( user.getEvents() );

    adapter.notifyDataSetChanged();}
    
answered by 13.05.2018 / 21:32
source