1

Created Objectbox class extends Application. This class used in Recyclerview Adapter and Activity. How it declared in RecyclerView? using Object Class I have stored data in ObjectBox database and Give this error:

java.lang.ClassCastException: android.app.Application cannot be cast to

Stored data in ObjectBox database using ImageView and ImageButton OnClickListener can it stored in object box database? please guide me.

public class ObjectBox extends Application{

    private static ObjectBox box;
    private BoxStore boxStore;

    @Override
    public void onCreate() {
        super.onCreate();

        boxStore = MyObjectBox.builder().androidContext(ObjectBox.this).build();
    }

    public static ObjectBox getBox(){
        return box;
    }

    public BoxStore getBoxStore(){
        return boxStore;
    }
}



public class ThumbnailAdapter extends RecyclerView.Adapter<ThumbnailAdapter.MyViewHolder>{


    private Context mContext;
    private List<Thumbnail> albumList;
    private List<Giphy> giphyList;

    private BoxStore boxStore;
    private Box<Giphy> box;



    public class MyViewHolder extends RecyclerView.ViewHolder {
       // public TextView title, count;

        @BindView(R.id.imgThumbnail)
        public ImageView imageView;

        @BindView(R.id.imgButtonThumbUp)
        public ImageButton imageButtonUP;

        @BindView(R.id.imgButtonThumbDown)
        public ImageButton imageButtonDown;

        @BindView(R.id.tvThumbUpCount)
        public TextView tvUpCount;

        @BindView(R.id.tvThumbDownCount)
        public TextView tvCountDown;

        int countUP = 0;
        int countDown = 0;

        String url;
        int id = 0;
        int thumbUp,thumbDown;

        public MyViewHolder(View view) {
            super(view);
            mContext = view.getContext();
            //imageView = (ImageView) view.findViewById(R.id.imgThumbnail);

            ButterKnife.bind(this,view);




            int idCount = id++;

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // String Title = title.getText().toString();

                    Thumbnail list = albumList.get(getAdapterPosition());

                    url = list.getVideoUrl();

                    Intent intent = new Intent(mContext,ExoPlayer.class);
                    intent.putExtra("url",list.getVideoUrl());


                    mContext.startActivity(intent);


                }
            });

            imageButtonUP.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                   thumbUp = countUP++;

                    //box.put(countUP);

                    tvUpCount.setText(""+countUP);
                }
            });

            imageButtonDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    thumbDown = countDown++;

                   // box.put(countDown);

                    tvCountDown.setText(""+countDown);
                }
            });

            addData(new Giphy(idCount,url,thumbUp,thumbDown));

        }



    }

    public ThumbnailAdapter(Context mContext, List<Thumbnail> albumList,List<Giphy> giphyList) {
        this.mContext = mContext;
        this.albumList = albumList;
        this.giphyList = giphyList;


        boxStore = ((ObjectBox)mContext.getApplicationContext()).getBoxStore();

        box = boxStore.boxFor(Giphy.class);

    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_list_item_album, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {


        Thumbnail album = albumList.get(position);



        Glide.with(mContext)
                .asGif()
                .load(album.getGif())
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))


                .into(holder.imageView);


    }

    @Override
    public int getItemCount() {
        return albumList.size();
    }

    public void addData(Giphy giphy){
        box.put(giphy);
    }
}
letsintegreat
  • 3,328
  • 4
  • 18
  • 39
rahul
  • 87
  • 2
  • 8
  • 1
    Did you mentioned your `ObjectBox` class as application in your manifest? – Ircover Sep 21 '18 at 06:29
  • no mention in Manifest file. How to it mention – rahul Sep 21 '18 at 06:31
  • java.lang.ClassCastException: android.app.Application cannot be cast to com.zala.giphyvideo.Database.ObjectBox at com.zala.giphyvideo.ThumbnailAdapter.(ThumbnailAdapter.java:139) – rahul Sep 21 '18 at 06:37
  • 1
    About manifest you can find answer [here](https://stackoverflow.com/a/2929927/4762282). – Ircover Sep 21 '18 at 06:38

1 Answers1

1

In your Manifest file (AndroidManifest.xml), you should add the ObjectBox class like an application name, in my case the ObjectBox class is called App, and in the Manifest just add in the tag application in the attribute name:

Example

 <application
        android:name=".**YourObjectBoxClass**">
...
</aplication>

Screenshot Manifest

Matthijs
  • 2,483
  • 5
  • 22
  • 33