来自Firebase的数据未显示在RecyclerView上

时间:2016-06-02 09:31:09

标签: android firebase android-recyclerview

我正在尝试显示来自Firebase数据库的数据,但我得到以下信息: my recyclerview

Firebase database.

我一直在寻找不同的解决方案,但我仍然会回到同样的问题。我对Android和编程都很新,所以任何帮助都会受到赞赏。这是相关的代码。

LogbookFragment:

public class LogbookFragment extends Fragment {

private TextView textView;
private CardView cardView;
private String assessment;
private String details;

private RecyclerView rv;
RequestQueue requestQueue;

private List<Assessment> skillsList = new ArrayList<>();

private static final String ARG_SECTION_NUMBER = "position";

/**
 * Returns a new instance of this fragment for the given section
 * number.
 */
public static LogbookFragment newInstance(int position) {
    LogbookFragment fragment = new LogbookFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, position);
    fragment.setArguments(args);
    return fragment;
}

public LogbookFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_logbook, container, false);

    Context context = getActivity();
    cardView = (CardView) rootView.findViewById(R.id.cv);
    rv = (RecyclerView) rootView.findViewById(R.id.rv);

    final LinearLayoutManager llm = new LinearLayoutManager(context);
    rv.setLayoutManager(llm);

    final RVAdapter adapter = new RVAdapter(skillsList);
    rv.setAdapter(adapter);

    initializeData();

    return rootView;
}

private void initializeData(){
    skillsList = new ArrayList<>();
    Firebase ref = new Firebase("https://athenajs.firebaseio.com/assessments");

    final RVAdapter adapter = new RVAdapter(skillsList);


    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            System.out.println(dataSnapshot.getValue());

            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                for (int i = 0; i<42; i++) {
                    assessment = postSnapshot.child("assessment").getChildren().toString();
                    details = postSnapshot.child("details").getChildren().toString();

                    skillsList.add(new Assessment(assessment, details));
                    rv.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }

            }

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });
}

}

RVAdapter:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.AssessViewHolder> {



public static class AssessViewHolder extends RecyclerView.ViewHolder {

    CardView cv;
    TextView assessName;
    TextView pracName;

    AssessViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cv);
        assessName = (TextView)itemView.findViewById(R.id.assessment);
        pracName = (TextView) itemView.findViewById(R.id.details);

    }
}

List<Assessment> skill;

RVAdapter(List<Assessment> skills){
    this.skill = skills;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public AssessViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemlogbook, viewGroup, false);
    AssessViewHolder avh = new AssessViewHolder(v);
    return avh;
}

@Override
public void onBindViewHolder(AssessViewHolder assessViewHolder, int i) {
    assessViewHolder.assessName.setText(skill.get(i).assessment);
    assessViewHolder.pracName.setText(skill.get(i).details);
}

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

}

}

评估班:

public class Assessment {
String assessment;
String details;

Assessment(String assessment, String details) {
    this.assessment = assessment;
    this.details = details;

}

public String getAssessment() {
    return assessment;
}

public String getDetails() {
    return details;
}

2 个答案:

答案 0 :(得分:3)

正如评论中所述,问题可能在于您只是向已经初始化适配器的skillsList AFTER 添加数据 - 而且skillsList一开始是空的,没有什么可以展示的。 因此,您实际上并未更改基础数据,而是使用第一个初始数据初始化适配器 - 这些是您应对initializeData()方法所做的更改:

private void initializeData(){
    skillsList = new ArrayList<>();
    Firebase ref = new Firebase("https://athenajs.firebaseio.com/assessments/skills");

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            System.out.println(dataSnapshot.getValue());

            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                for (int i = 0; i<42; i++) {
                    assessment = postSnapshot.child("assessment").getValue().toString();
                    details = postSnapshot.child("details").getValue().toString();
                    //here you add the assessment object to the list
                    skillsList.add(new Assessment(assessment, details));                    
                }

            }
            //at this point you have looped through all the "children" - 
            //now you are ready to initialize the adapter
            RVAdapter adapter = new RVAdapter(skillsList);
            rv.setAdapter(adapter));
            //commented out: no need to call this now - since you have not made any changes to the data
            //adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });
}

我希望这会有所帮助。试一试,让我知道。

答案 1 :(得分:0)

我认为这两行引起了问题

 assessment = postSnapshot.child("assessment").getChildren().toString();
 details = postSnapshot.child("details").getChildren().toString();

根据firebase文档,您应该通过调用

来检索您的值
getValue() 

方法不

toString();

因为我不知道您的评估和详细数据是如何的。如果它是单个值那么

assessment = postSnapshot.child("assessment").getValue(String.class);
     details = postSnapshot.child("details")..getValue(String.class);

会做到这一点。

有关详细信息,请转到here