未连接适配器;使用recyclerView跳过布局

时间:2018-12-18 16:13:23

标签: android android-studio android-recyclerview android-adapter recycler-adapter

我有一个Firebase数据库,正在从中获取数据,但是我无法显示获取的数据(某些文本和img)

这是我在片段中的代码:

public class Tab_0 extends Fragment {
RecyclerView mRecyclerView;
    FirebaseDatabase mFirebaseDataBase;
    DatabaseReference mRef;
    DatabaseReference mRefA;

    @SuppressLint("RestrictedApi")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){

        View view = inflater.inflate(R.layout.tab0,container,false);

        mRecyclerView = view.findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    mRecyclerView.setHasFixedSize(true);

    mFirebaseDataBase = FirebaseDatabase.getInstance();
    mRef = FirebaseDatabase.getInstance().getReferenceFromUrl("--my url here--");
    mRef = mRef.child("pictures");

    FirebaseRecyclerAdapter<model,ViewHolder> FirebaseRecyclerAdapter = new FirebaseRecyclerAdapter<model,ViewHolder>(
                    model.class,
                    R.layout.card,
                    ViewHolder.class,
                    mRef
            ){
                @Override
                protected void populateViewHolder(ViewHolder viewHolder,model model,int position){
                    viewHolder.setDetails( getContext() , model.getImage());
                }
            };
    mRecyclerView.setAdapter(FirebaseRecyclerAdapter);

    return view;
}

,我是从LogCat那里得到的

  

未附加适配器,跳过布局

我之前在建议中(在另一个问题中)在 onCreateView 方法之外使用了此方法,但是什么也没有

@Override
public void onStart(){
    super.onStart();
    FirebaseRecyclerAdapter<model,ViewHolder> FirebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<model,ViewHolder>(
                    model.class,
                    R.layout.card,
                    ViewHolder.class,
                    mRef
            ){
                @Override
                protected void populateViewHolder(ViewHolder viewHolder,model model,int position){
                    viewHolder.setImage( getContext() , model.getImage());
                }
            };
            mRecyclerView.setAdapter(FirebaseRecyclerAdapter);
}

但什么都没显示

viewHolder

public class ViewHolder extends RecyclerView.ViewHolder {


View mView;

public ViewHolder(@NonNull View itemView) {
    super(itemView);
    mView = itemView;
}

public void setDetails(Context ctx, String image){
    ImageView img = mView.findViewById(R.id.img);
    Picasso.get().load(image).into(img);
}}

我在模型类上设置了getter和setter方法。(对于持有者类,该变量是必需的)

1 个答案:

答案 0 :(得分:1)

好,我知道了

tab_0 xml文件是这样的:

        tools:context=".Tab_0"

解决方案是我没有提供相关内容:

{{1}}

感谢 StackOverFlowrs!