0
votes

Its impossible for me fixing this problem which just appeared now.. everything worked perfectly before.... this is the error:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.nicco.wallpapersapp.GridItem at com.google.android.gms.internal.zzbtg.zze(Unknown Source) at com.google.android.gms.internal.zzbtg.zzb(Unknown Source) at com.google.android.gms.internal.zzbtg.zza(Unknown Source) at com.google.firebase.database.DataSnapshot.getValue(Unknown Source) at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:151) at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:140) at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:183) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515) at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410) at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1710) at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:346) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874) at android.view.Choreographer.doCallbacks(Choreographer.java:686) at android.view.Choreographer.doFrame(Choreographer.java:618) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Application terminated.

my class:

public class HomePage extends AppCompatActivity {

private DrawerLayout mDraw;
private android.support.v7.app.ActionBarDrawerToggle mToggle;

private DatabaseReference mDatabase;

private RecyclerView recycler;

private Toolbar mToolbar;

private NavigationView nView;


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

    mDatabase = FirebaseDatabase.getInstance().getReference("landscape");

    recycler = findViewById(R.id.recyclerView);
    recycler.setLayoutManager(new LinearLayoutManager(this));
    //layoutManager = new GridLayoutManager(this,2);
    //recycler.setLayoutManager(layoutManager);
    recycler.setHasFixedSize(true);

    mToolbar = findViewById(R.id.toolbarNature);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("Landscape Wallpapers");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mDraw = findViewById(R.id.drawerLayout);
    mToggle = new android.support.v7.app.ActionBarDrawerToggle(this, mDraw, mToolbar, R.string.open, R.string.close);

    mDraw.addDrawerListener(mToggle);
    mToggle.syncState();

    nView = findViewById(R.id.navigViewNature);

    nView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            int id = item.getItemId();

            if(id == R.id.landscape_area){

                startActivity(getIntent());
            }

            if(id == R.id.wallMoment){

                startActivity(new Intent(HomePage.this, AppMoment.class));
            }

            if(id == R.id.minimal_area){

                //startActivity(new Intent(AppMoment.this, AppMoment.class));
            }

            if(id == R.id.city_area){

                //startActivity(new Intent(AppMoment.this, AppMoment.class));
            }

            if(id == R.id.mountain_area){

                //startActivity(new Intent(AppMoment.this, AppMoment.class));
            }

            if(id == R.id.beach_area){

                //startActivity(new Intent(AppMoment.this, AppMoment.class));
            }

            if(id == R.id.sea_area){

                //startActivity(new Intent(AppMoment.this, AppMoment.class));
            }

            if(id == R.id.cars_area){

                //startActivity(new Intent(AppMoment.this, AppMoment.class));
            }

            if(id == R.id.bikes_area){

                //startActivity(new Intent(AppMoment.this, AppMoment.class));
            }

            return false;
        }
    });

}

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

    FirebaseRecyclerAdapter<GridItem, MyViewHoler> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<GridItem, MyViewHoler>(

            GridItem.class,
            R.layout.gridview_item_layout,
            MyViewHoler.class,
            mDatabase
    ) {
        @Override
        protected void populateViewHolder(MyViewHoler viewHolder, GridItem model, int position) {

            final String imageData = getRef(position).getKey();

            viewHolder.setImage(getApplicationContext(), model.getImage());


        }
    };

    recycler.setAdapter(firebaseRecyclerAdapter);
    recycler.setHasFixedSize(true);

    }


public static class MyViewHoler extends RecyclerView.ViewHolder {

    View mView;


    public MyViewHoler(View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void setImage(Context ctx, String image){

        ImageView mImage = mView.findViewById(R.id.item_image);
        //Picasso.with(ctx).load(image).into(mImage);
        Glide.with(ctx).load(image).into(mImage);
    }
  }
}

My GridItem class:

public class GridItem  {

   private String image;

   public GridItem(){

   }

public GridItem(String image) {
    this.image = image;
   }

public String getImage() {
    return image;
   }

public void setImage(String image) {
    this.image = image;
   }
 }

This is the Database:

wallpapaersapp
-beach
-bikes
-cars
-city
-landscape
        image: "https://firebasestorage.googleapis.com/v0/b/wal..."
-minimal
-mountain
-sea
-wallmoment
2
Please edit your question to show the contents of the database at the location of your reference: /landscape. - Doug Stevenson
"everything worked perfectly before" ? Before what? - Barns
@DougStevenson done, in the end I typed the database on Firebase - Nicco
@Barns yeah everything was working fine.. I only changed the Database by adding nodes such as landscape and inside that, I posted data, in this case images, I just edited the post, look in the end I wrote the database - Nicco
@DougStevenson its like it doesnt recognize the image url into the database, because it says it cant convert it to a String - Nicco

2 Answers

0
votes

FirebaseUI is performing your query to find a list of child items under /landscape to display. Your code is assuming that each child can be mapped to type GridItem.

However, right now, your that location has a single child whose value is a String. A single String cannot be mapped to a Java object. That's where your error is coming from. Change the structure of your database to add that extra level of child object that can map to your data model, or change your FirebaseUI type mapping to String.

If you want to use GridItem, your database should look like this:

-landscape
  - child1
    - image: "https://firebasestorage.googleapis.com/v0/b/wal..."
  - child2
    - image: "https://firebasestorage.googleapis.com/v0/b/wal..."
0
votes

Just add toString() here

viewHolder.setImage(getApplicationContext(), model.getImage().toString());

or you can import picasso and add this

compile 'com.squareup.picasso:picasso:2.5.2'

and then

  @Override
        protected void populateViewHolder(MyViewHoler viewHolder, GridItem model, int position) {

            final String imageData = getRef(position).getKey();

           // viewHolder.setImage(getApplicationContext(), model.getImage());


        }
    };
Picasso.with(viewHolder.imageView.getContext()).load(model.getUrl()).into(viewHolder.imageView);  //where imageView is where you want to put your firebase image
                    }
                };