[英]Issues retrieving data from database in Realtime Firebase
我目前正在制作一個餐廳菜單應用程序,並且我遵循了一些在線教程,該人能夠在他的應用程序中顯示他的實時 firebase 中的內容。 目前,我可以通過我的應用程序(反映在我的數據庫本身中)將新菜單項添加到我的數據庫中,但在應用程序本身中,我實際上無法檢索數據庫。 我的代碼中是否有任何錯誤? 任何幫助將不勝感激!
'''
EditText editTextItemName, editTextPrice;
Spinner spinnerSection;
ListView ListViewItems;
Button buttonMainMenu, buttonAddItem;
List<Items> listItems;
DatabaseReference databaseItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
databaseItems = FirebaseDatabase.getInstance().getReference("items");
listItems = new ArrayList<>();
//ItemActivity ItemAdapter = new ItemActivity(Main2Activity.this,listItems);//
//ListViewItems.setAdapter(ItemAdapter);//
buttonMainMenu = (Button) findViewById(R.id.buttonMainMenu);
buttonMainMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openMainActivity();
}
});
buttonAddItem = (Button) findViewById(R.id.buttonAddItem);
editTextItemName = (EditText) findViewById(R.id.editTextItemName);
editTextPrice = (EditText) findViewById(R.id.editTextPrice);
spinnerSection = (Spinner) findViewById(R.id.spinnerSection);
ListViewItems = (ListView) findViewById(R.id.ListViewItems);
buttonAddItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addItems();
}
});
ListViewItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Items Items = listItems.get(i);
showUpdateDeleteDialog(Items.getItemsId(), Items.getItemName());
return true;
}
});
}
@Override
protected void onStart() {
super.onStart();
databaseItems.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listItems.clear();
for (DataSnapshot itemSnapshot : dataSnapshot.getChildren()) {
Items items = itemSnapshot.getValue(Items.class);
listItems.add(items);
}
ItemActivity adapter = new ItemActivity(Main2Activity.this, listItems);
ListViewItems.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
private void addItems(){
String Name = editTextItemName.getText().toString().trim();
String Price = editTextPrice.getText().toString().trim();
String Section = spinnerSection.getSelectedItem().toString();
if (ItemName.isEmpty()) {
editTextItemName.setError("Item Name can't be empty");
editTextItemName.requestFocus();
return;
}
if (Price.isEmpty()) {
editTextPrice.setError("Price can't be empty");
editTextPrice.requestFocus();
return;
} else {
String id = databaseItems.push().getKey();
Items items = new Items(id, ItemName, Section, Price);
databaseItems.child(id).setValue(items);
Toast.makeText(this, "Item Added", Toast.LENGTH_LONG).show();
}
}
public void openMainActivity(){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
private void showUpdateDeleteDialog(final String ItemsId, String ItemName) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_update_items, null);
dialogBuilder.setView(dialogView);
final EditText editTextItemName = (EditText) dialogView.findViewById(R.id.editTextItemName);
final EditText editTextPrice = (EditText) dialogView.findViewById(R.id.editTextPrice);
final Spinner spinnerSection = (Spinner) dialogView.findViewById(R.id.spinnerSection);
final Button buttonUpdateItem = (Button) dialogView.findViewById(R.id.buttonUpdateItem);
final Button buttonDeleteItem = (Button) dialogView.findViewById(R.id.buttonDeleteItem);
dialogBuilder.setTitle(ItemName);
final AlertDialog b = dialogBuilder.create();
b.show();
buttonUpdateItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ItemName = editTextItemName.getText().toString().trim();
String ItemPrice = editTextPrice.getText().toString().trim();
String ItemSection = spinnerSection.getSelectedItem().toString();
if (ItemName.isEmpty()) {
editTextItemName.setError("Item Name can't be empty");
editTextItemName.requestFocus();
return;
}
if (ItemPrice.isEmpty()) {
editTextPrice.setError("Price can't be empty");
editTextPrice.requestFocus();
return;
}
else updateItems(ItemsId, ItemName, ItemSection, ItemPrice);
}
});
buttonDeleteItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteItems(ItemsId);
}
});
}
private boolean updateItems(String id, String Name, String Section, String Price){
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("items").child(id);
Items items = new Items(id, Name, Section, Price);
databaseReference.setValue(items);
Toast.makeText(this, "Item Updated", Toast.LENGTH_LONG).show();
return true;
}
private void deleteItems(String ItemsId) {
DatabaseReference drItems = FirebaseDatabase.getInstance().getReference("items").child(ItemsId);
drItems.removeValue();
Toast.makeText(this, "Item Deleted", Toast.LENGTH_LONG).show();
}
} '''
這將是一段漫長的旅程,請坐。
首先,我想用自然數替換數據庫 ID。 為此,您需要更改添加新項目的方式:
創建一個 class AddNewItems:
package com.<package-name>;
public class AddNewItems {
private String name;
private String section;
private String price;
private int id;
public AddNewItems() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
現在讓我們回到添加新項目的 MainActivity。 將您的addItem()
修改為:
AddNewItems addNewItems; //Add this below your DatabaseReference databaseItems;
private void addItems(){
String Name = editTextItemName.getText().toString().trim();
String Price = editTextPrice.getText().toString().trim();
String Section = spinnerSection.getSelectedItem().toString();
String lastItemIdFetched; //This will check ID of last item and assign new value to new items.
Int lastIdInt; //For adding 1, we need to convert to int
//Make sure you read the note at bottom
if (ItemName.isEmpty()) {
editTextItemName.setError("Item Name can't be empty");
editTextItemName.requestFocus();
return;
}
if (Price.isEmpty()) {
editTextPrice.setError("Price can't be empty");
editTextPrice.requestFocus();
return;
} else {
//For sake of automating new ID
DatabaseReference addItemIdRef;
addItemIdRef = FirebaseDatabase.getInstance().getReference("itemIDs");
addItemIdRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
lastItemIDFetched = dataSnapshot.child("lastItemID").getValue().toString();
lastIdInt = Integer.parseInt(lastItemIdFetched);
lastIdInt = lastIdInt + 1;
}catch (Throwable e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
// Adding data in database
addNewItems.setName(Name);
addNewItems.setSection(Section);
addNewItems.setPrice(Price);
addNewItems.setId(lastIdInt);
databaseItems.child(lastIdInt).setValue(addNewItems);
Toast.makeText(getApplicationContext(), "New Item has been added to your menu.", Toast.LENGTH_SHORT).show();
// We also need to value of last ID in database. So do this.
Map<String, Object> updateNewID = new HashMap<>();
updateNewID.put("lastItemID", lastIdInt);
addItemIdRef.child("lastItemID).updateChildren(updateNewID).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "Success on updating", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
});
//WE have successfully updated the last item ID
}
}
注意:當我們從數據庫中獲取lastItemID
時。 沒有代碼可以驗證這是否是第一項,並且可能導致崩潰。 如果不想增加代碼(它已經是),請從控制台手動創建以下代碼。
如圖所示創建一個新節點並將值設置為 0,因此當您添加第一個項目時,它的 ID 將為 1。
現在回到檢索部分,在onStart()
function 中執行此操作:讓我們使用列表來執行此操作:
//Create a listView in XML also and define it in MainActivity.
//ListView listItems = (ListView) findViewById(R.id.resource_name);
//First create a list and adapter
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
DatabaseReference fetchItemsRef;
fetchItemsRef = FirebaseDatabase.getInstance().getReference("items");
DatabaseReference getTotalNumberOfItemsRef;
getTotalNumberOfItemsRef = FirebaseDatabase.getInstance().getReference("itemIDs");
Int i, totalNumberOfItems;
//I've again added this to check number items available.
//Put this in your `onStart()` function.
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
//Get total number of items:
getTotalNumberOfItemsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
totalNumberOfItems = Integer.parseInt(dataSnapshot.child("lastItemID").getValue().toString());
}catch (Throwable e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//Now let's fetch the data from Firebase using a `for loop`:
for(int i=0; i<=totalNumberOfItems; i++){
String currentItemName = "item"+i;
fetchItemsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
currentItemName = dataSnapshot.child(i).getValue().toString();
}catch (Throwable e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
listItems.add(currentItemName);
adapter.notifyDataSetChanged();
}
現在您必須確保數據庫中的所有項目都是連續整數,例如 1,2,3... 並且不能像 1,2,4,5,8... 因為沒有代碼可以處理 null 對象. 您可以稍后使用條件語句添加它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.