NewKidOnTheBlock
NewKidOnTheBlock

Reputation: 283

RecyclerView Not Displaying Data Received

I am getting the data from Firebase (online data storage) but somehow in my adapter class, the dataset is []. However when I pass in 20 random things using the same class, it works perfectly. Here is the code.

Fragment Class:

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

Adapter Class:

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

Does anyone have an idea to why this is happening, why my data from Firebase is not showing up?

Thanks

Upvotes: 3

Views: 1439

Answers (1)

N.T.
N.T.

Reputation: 2611

In onCreate you asynchronously go get the data - which might take some time. In the meantime the program flow moves along and you set up the recycler in onCreateView, but this happens before you get the response with data, and as a result your mDataset is still without values. A while after you get the result and put it in mDataset, but I don't see a place where you notify the RecyclerView about the change.

So you either have to defer setting up the RecyclerView until after you really get the results or notify the RecyclerView about the data set update once you get it.

Upvotes: 2

Related Questions