RecyclerView不显示收到的数据

时间:2015-08-16 22:40:56

标签: java android fragment firebase android-recyclerview

我从Firebase(在线数据存储)获取数据,但在我的适配器类中,数据集是[]。然而,当我使用相同的类传递20个随机的东西时,它完美地工作。这是代码。

片段类:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myFirebaseRef = new Firebase("http...");
    initListItems();
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_products, container, false);
    // Inflate the layout for this fragment
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));

    mAdapter = new MyAdapter(mDataset);
    mRecyclerView.setAdapter(mAdapter);


    return rootView;

}
// Get the data from Firebase and set it to List
private void initListItems() {

    // Attach an listener to read the data at our posts reference
    myFirebaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            System.out.println("There are " + snapshot.getChildrenCount() + "children");

            for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                title = postSnapshot.child("name").getValue().toString();
                description = postSnapshot.child("description").getValue().toString();
                location = postSnapshot.child("location").getValue().toString();
                //This displays the information so it is properly reading data in
                System.out.println("title: " + title  +
                        "description" + description + "location" + location);

                mDataset.add(new ListItem(title, description, location, 0.00, 0.00));


            }

        }
    });
    // If i remove the above code and just put this, the recycleview displays this information
    for (int i = 0; i<20; i++) {
        String name ="This is element #" + i;
        mDataset.add(new ListItem(name, "description", "location", 0.00, 0.00));
    }
}

适配器类:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private static final String TAG = "MyAdapter";
    private List<ListItem> mDataSet;
    /**
     * Provide a reference to the type of views that you are using (custom ViewHolder)
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;
        private final ImageView imageView;
        private final TextView textViewLocation;

        public ViewHolder(View v) {
            super(v);
            // Define click listener for the ViewHolder's View.
            v.setOnClickListener(new View.OnClickListener() {
                ...
            });
            textView = (TextView) v.findViewById(R.id.startupName);
            textViewLocation = (TextView) v.findViewById((R.id.startupLocation));
            imageView = (ImageView) v.findViewById(R.id.startupImage);
        }

        public TextView getTextView() {
            return textView;
        }
        public TextView getTextViewLocation() {
            return textViewLocation;
        }
        public ImageView getImageView() {
            return imageView;
        }

    }



//    When I try to send the data received from Firebase, it prints out [ ] but if i use the random data I created, it works              
public MyAdapter(List<ListItem> dataSet) {
        System.out.println("THE DATASET IS " + dataSet);
        mDataSet = dataSet;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // Create a new view.
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.row_item, viewGroup, false);

        return new ViewHolder(v);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        Log.d(TAG, "Element " + position + " set.");

        // Get element from your dataset at this position and replace the contents of the view with that element
        ...

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataSet == null ? 0 : mDataSet.size();
    }
}

This is picture of recyclerview with the random data

This is picture of recyclerview with data from firebase

有没有人知道为什么会这样,为什么我的Firebase数据没有显示?

由于

1 个答案:

答案 0 :(得分:2)

onCreate中,您异步获取数据 - 这可能需要一些时间。与此同时,程序流程随之移动,并且您在onCreateView中设置了回收站,但这会在您收到数据响应之前发生,因此您的mDataset仍然没有值。在您获得结果并将其放入mDataset后一段时间,但我没有看到您通知RecyclerView有关更改的地方。

因此,您必须推迟设置RecyclerView,直到获得结果为止,或者在获得结果后通知RecyclerView数据集更新。