0

I have the following code for a recyclerview using Firebase. The Recycler View uses the following adapter.

FirebaseRecyclerAdapter<DownloadList,DownloadListFragment.DownloadViewHolder> recyclerAdapter= new FirebaseRecyclerAdapter<DownloadList, DownloadViewHolder>(
            DownloadList.class,
            R.layout.download_cardview,
            DownloadViewHolder.class,
            databaseReference
    ) {
        @Override
        protected void populateViewHolder(DownloadViewHolder viewHolder, DownloadList model, int position) {
            viewHolder.setTitle(model.gettitle());
            //viewHolder.setImage(model.getimage());
            viewHolder.setDate(model.getDate());
        }


        @Override
        public DownloadViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            DownloadViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
            viewHolder.setOnClickListener(new DownloadViewHolder.ClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Toast.makeText(getActivity(), "Item click at " + arrayAdapter.getItem(position), Toast.LENGTH_SHORT).show();

                    DownloadImageFragment fragment = new DownloadImageFragment();
                    Bundle arguments = new Bundle();
                    arguments.putString( "Batch" , batchID);
                    arguments.putString( "Type" , typeID);
                    arguments.putString( "Url", arrayAdapter.getItem(position));
                    arguments.putString( "Title", (String) titleAdapter.getItem(position));

                    fragment.setArguments(arguments);
                    final FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.replace(R.id.content, fragment , "Download List");
                    ft.commit();

                }

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

            return viewHolder;
        }
    };

    recyclerView.setAdapter(recyclerAdapter);


    return view;
}

public static class DownloadViewHolder extends RecyclerView.ViewHolder {
    View mView;
    TextView textView_Title;
    TextView textView_Date;
    TextView textView_Image;

    String simage,stitle;

    private static Context context;

    public DownloadViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        context = itemView.getContext();
        textView_Title = (TextView) itemView.findViewById(R.id.textView_download_title);
        textView_Image = (TextView) itemView.findViewById(R.id.textView_image_download);
        textView_Date = (TextView) itemView.findViewById(R.id.textView_download_date);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onItemClick(v, getAdapterPosition());

            }
        });
        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mClickListener.onItemLongClick(v, getAdapterPosition());
                return true;
            }
        });
    }

The getters and setters are defined as :

public class DownloadList {
private String date;
private String title;
private String image;

public DownloadList(){}

public DownloadList(String date, String title, String image){
    this.date = date;
    this.title = title;
    this.image = image;
}

public String getDate(){
    return date;
}

public void setDate(String date){
    this.date = date;
}

public String gettitle(){
    return title;
}

public void setTitle(String title){
    this.title = title;
}

public String getimage(){
    return image;
}

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

Now on building the apk the following error pops up

error: constructor FirebaseRecyclerAdapter in class FirebaseRecyclerAdapter cannot be applied to given types; required: FirebaseRecyclerOptions found: Class,int,Class,DatabaseReference reason: actual and formal argument lists differ in length where T,VH are type-variables: T extends Object declared in class FirebaseRecyclerAdapter VH extends ViewHolder declared in class FirebaseRecyclerAdapter

This was not there before the 3.1 update of the android studio(March 2018).

I have event tried changing the app:compatv7 version but it still doesn't work.

Also according to the documentation provided FirebaseUI for Realtime Database it says to use

Query query = FirebaseDatabase.getInstance()
    .getReference()
    .child("chats")
    .limitToLast(50);

and

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

Now my question is since i have used the above recyclerView in 15 different places in the app so do i need to change this at all the 15 places or there is some other way around ? Also is there any surety that if I use query, the new code will be compatible ?

Github link for complete code

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
addy
  • 52
  • 1
  • 2
  • 5

1 Answers1

0

You could implement your own FirebaseRecyclerAdapter subclass that maps the old signature to the new signature. Something like:

class MyFirebaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder> :FirebaseRecyclerAdapter<T, VH> {
    public MyFirebaseRecyclerAdapter(Class pojoClass<T>, int layout, Class<VH> viewHolderClass, Query query) {            
        super(new FirebaseRecyclerOptions.Builder<T>()
            .setQuery(query, pojoClass.class)
            .build());
        ...

You'll then still need to update all places to use the new MyFirebaseRecyclerAdapter, but at least the signature will be compatible after that.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807