我在我的代码(if)中添加了一个逻辑方法,其中(如果Sqlite为空)调用firebase数据并将其保存到SQLite中,然后停止调用valueEventListener(如果不是)以获取SQLite.class的查询。它运作良好,但是当我完成活动并再次打开它时,过程再次开始。 这是更新后的代码
@Override
protected void onStart() {
super.onStart();
if (FB2sqLite == null) {
ArrayList<HashMap<String, String>> myList = FB2sqLite.getProducts();
if (myList.size() != 0) {
ListAdapter adapter = new SimpleAdapter(FireBase_Prices.this, myList, R.layout.model,
new String[]{item_Code, ItemDesc},
new int[]{R.id.ItemCodeView, R.id.descTxt});
listView.setAdapter(adapter);
}
} else {
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
pricesArrayList.clear();
for (DataSnapshot PricesListDataSnapshot : dataSnapshot.getChildren()) {
PricesList pricesList = PricesListDataSnapshot.getValue(PricesList.class);
pricesArrayList.add(pricesList);
FB2sqLite.insert(pricesList);
FB2sqLite.close();
}
ArrayList<HashMap<String, String>> myList = FB2sqLite.getProducts();
if (myList.size() != 0) {
ListAdapter adapter = new SimpleAdapter(FireBase_Prices.this, myList, R.layout.model,
new String[]{item_Code, ItemDesc},
new int[]{R.id.ItemCodeView, R.id.descTxt});
listView.setAdapter(adapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
如何使用 removeEventListener ,该方法的位置在哪里?