错误:类FirebaseRecyclerAdapter <t,vh>中的构造函数FirebaseRecyclerAdapter无法应用于给定类型

时间:2018-04-22 02:51:18

标签: android firebase firebase-realtime-database recycler-adapter firebaseui

我有使用Firebase的recyclerview的以下代码。 Recycler View使用以下适配器。

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;
            }
        });
    }

getter和setter定义为:

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;
}
}

现在,在构建apk时会弹出以下错误

  

错误:类中的构造函数FirebaseRecyclerAdapter   FirebaseRecyclerAdapter无法应用于给定类型;   必需:找到FirebaseRecyclerOptions:   类,INT,类,DatabaseReference   原因:实际和正式的参数列表长度不同,其中T,VH   是类型变量:T扩展在类中声明的Object   FirebaseRecyclerAdapter VH扩展了在类中声明的ViewHolder   FirebaseRecyclerAdapter

在Android工作室的3.1更新(2018年3月)之前,这不存在。

我有尝试更改应用的事件:compatv7版本,但它仍然无法正常工作。

另外根据提供的文档FirebaseUI for Realtime Database,它说要使用

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

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

现在我的问题是,因为我在应用程序的15个不同位置使用了上面的recyclerView所以我需要在所有15个地方更改这个或还有其他方法?还有任何保证,如果我使用查询,新代码将兼容?

Github link for complete code

1 个答案:

答案 0 :(得分:0)

您可以实现自己的FirebaseRecyclerAdapter子类,将旧签名映射到新签名。类似的东西:

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());
        ...

然后,您仍然需要更新所有地点以使用新的MyFirebaseRecyclerAdapter,但至少签名将在此之后兼容。