gpt4 book ai didi

java - RecyclerView 不显示接收到的数据

转载 作者:太空狗 更新时间:2023-10-29 13:18:24 25 4
gpt4 key购买 nike

我正在从 Firebase(在线数据存储)获取数据,但不知何故在我的适配器类中,数据集是 []。然而,当我使用同一个类传入 20 个随机的东西时,它工作得很好。这是代码。

fragment 类:

@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 数据没有显示出来?

谢谢

最佳答案

onCreate你异步去获取数据——这可能需要一些时间。与此同时,程序流程继续进行,您在 onCreateView 中设置了回收器。 ,但这发生在您获得数据响应之前,因此您的 mDataset仍然没有值(value)。得到结果后将其放入 mDataset 中。 ,但我没有看到您通知 RecyclerView 的地方关于变化。

因此您要么必须推迟设置 RecyclerView直到你真正得到结果或通知RecyclerView之后关于获取数据集后的更新。

关于java - RecyclerView 不显示接收到的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32040628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com