片段未显示RecyclerView

时间:2019-05-12 20:55:37

标签: android firebase-realtime-database android-recyclerview

因此,我观看了一些有关如何主要this进行RecyclerView的教程。我调整了代码以适应我的需求。但是,当我尝试运行它时,RecyclerView没有显示在片段上。我的猜测是问题出在适配器和Viewholder部分,因为我不确定如何实现它。谁能帮我吗?

Fragment.java

public class JobListFragment extends Fragment {

RecyclerView mRecyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;

public JobListFragment(){

}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ((MainHomeActivity)getActivity()).setActionBarTitle("Job List");

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

    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference().child("Data");

    mRecyclerView = view.findViewById(R.id.recycleView);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    mRecyclerView.setHasFixedSize(true);

    return view;
}

@Override
public void onStart() {
    super.onStart();
    FirebaseRecyclerOptions<Model> options =
            new FirebaseRecyclerOptions.Builder<Model>()
            .setQuery(databaseReference, Model.class).build();

    FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.row, viewGroup, false);

            return new ViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Model model) {
            holder.setDetails(model.getTitlept(), model.getDescpt(), model.getImagept());
        }
    };
    mRecyclerView.setAdapter(adapter);
}
}

ViewHolder.java

public class ViewHolder extends RecyclerView.ViewHolder {

View mView;

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

    mView = itemView;
}

public void setDetails(String title, String description, String image){
    TextView mTitleTv = mView.findViewById(R.id.rTitleTv);
    TextView mDescTv = mView.findViewById(R.id.rDescIv);
    ImageView mImageIv = mView.findViewById(R.id.rImageIv);

    mTitleTv.setText(title);
    mDescTv.setText(description);
    Picasso.get().load(image).into(mImageIv);
}
}

Model.java

public class Model {

String titlept, imagept, descpt;

public Model(){}

public String getTitlept() {
    return titlept;
}

public void setTitlept(String titlept) {
    this.titlept = titlept;
}

public String getImagept() {
    return imagept;
}

public void setImagept(String imagept) {
    this.imagept = imagept;
}

public String getDescpt() {
    return descpt;
}

public void setDescpt(String descpt) {
    this.descpt = descpt;
}
}

1 个答案:

答案 0 :(得分:0)

您能更清楚地写出问题吗?图片未显示? @AdhityoPutro