繁体   English   中英

RecyclerView不显示收到的数据

[英]RecyclerView Not Displaying Data Received

我从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();
    }
}

这是recyclerview与随机数据的图片

这是来自firebase的数据的recyclerview图片

有没有人知道为什么会发生这种情况,为什么我的Firebase数据没有出现?

谢谢

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

因此,您必须推迟设置RecyclerView直到您真正得到结果,或者在获得结果后通知RecyclerView有关数据集更新的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM