3
我從Firebase(在線數據存儲)獲取數據,但以某種方式在我的適配器類中,數據集爲[]。然而,當我使用同一個類通過20個隨機事物時,它完美地工作。這是代碼。RecyclerView不顯示收到的數據
片段類:
@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();
}
}
有沒有人有一個想法,爲什麼發生這種情況,爲什麼從火力地堡我的數據沒有出現?
感謝
這是正確的,所以在''onDataChange'''mDataset.add()'也調用'notifyDataSetChanged()'後。請注意,我們正在爲FirebaseUI添加一個RecyclerView適配器來解決這個問題:https://github.com/firebase/FirebaseUI-Android(0.2應該完成RecyclerViewAdapter合同,它現在在一個分支上)。 –
非常感謝Frank。我只需要添加mAdapter.notifyDataSetChanged();在mDataset.add(新的啓動(標題,說明,位置,0.00,0.00))之後; – NewKidOnTheBlock