gpt4 book ai didi

android - Firebase DB 无法显示查询的对象列表

转载 作者:行者123 更新时间:2023-11-29 02:36:12 25 4
gpt4 key购买 nike

出于某种原因,我的 Adapter 没有获得更新的对象列表,经过一些调试后我发现了这一点:当我在UpdateUI(),由于某种原因查询不会启动并返回一个空列表。也许我按错误的顺序做某事? (只是一些随机文本,因为我无法发布问题)

public class ProductListFragment extends Fragment {
private RecyclerView mRecyclerView;
private ListFragmentAdapter mAdapter;
private List<Product> mProducts;

public ProductListFragment() {
// Required empty public constructor
}
public static ProductListFragment newInstance() {
return new ProductListFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_product_list, container, false);
mRecyclerView = (RecyclerView)view.findViewById(R.id.listRecyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}

@Override
public void onStart() {
super.onStart();
updateUI();
}

private void updateUI() {
mProducts = DbOps.get(getActivity()).getProducts();

if (mAdapter == null) {
mAdapter = new ListFragmentAdapter(mProducts);
mRecyclerView.setAdapter(mAdapter);
} else {
mAdapter.notifyDataSetChanged();
}
}


public class DbOps {
private static DbOps sDbOps;
private FirebaseDatabase mDb;
private DatabaseReference dbRef;
private List<Product> mProducts;
private Product pr;

public static DbOps get(Context ctx) {
if (sDbOps == null) {
sDbOps = new DbOps(ctx);
}
return sDbOps;
}

private DbOps(Context ctx) {
mDb = FirebaseDatabase.getInstance();
dbRef = mDb.getReference().child("products");
if(mProducts == null) {
getProducts();
}
}

public Product getProduct(final String mId) {
Query query = dbRef.child(mId);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
for (DataSnapshot obj: dataSnapshot.getChildren()) {
if(obj.getValue(Product.class).getId().equals(mId)) {
pr = obj.getValue(Product.class);
break;
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {

}
});
return pr;
}

public void addProduct(Product product) {
dbRef.child(product.getId()).setValue(product);
}


public List<Product> getProducts() {
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot mObject : dataSnapshot.getChildren());
mProducts = new ArrayList<>();
Product pr = dataSnapshot.getValue(Product.class);
mProducts.add(pr);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};
dbRef.addValueEventListener(listener);
return mProducts;
}

}

最佳答案

您不能以您的方式返回 Product 类的对象,由于 onDataChange() 方法的异步行为,它始终为空您正在尝试从数据库中获取数据。所以要解决这个问题,你有两个选择:

  1. 您需要在 onDataChange() 方法中使用和声明 pr 对象,如下所示:

    query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    if(dataSnapshot.exists()) {
    for (DataSnapshot obj: dataSnapshot.getChildren()) {
    if(obj.getValue(Product.class).getId().equals(mId)) {
    Product pr = obj.getValue(Product.class);
    //do what you want to do with pr
    }
    }
    }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {}
    });
  2. 如果您想在onDataChange() 方法 之外使用pr 对象的值,那么请参阅我在post 中的回答.

关于android - Firebase DB 无法显示查询的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46972840/

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