Firebase数据库查询一次只能检索一次

时间:2018-12-28 12:38:10

标签: android firebase firebase-realtime-database

我已经成功地为我的第一个活动检索了一次查询,但是在第二个我想使用该查询的活动中,它无法正常工作,有人可以帮忙解决吗?

这是我的Firebase数据库

"food" : {
    "-LTHH-aVp3zAkkUHrO70" : {
      "foodId" : "-LTHH-aVp3zAkkUHrO70",
      "foodName" : "Char Keoy Tiaw",
      "foodType" : "Breakfast"
    },
    "-LTHWNfyDCzGoMSASjeP" : {
      "foodId" : "-LTHWNfyDCzGoMSASjeP",
      "foodName" : "Sandwich",
      "foodType" : "Breakfast"
    },
    "-LTHdfW1oNVz9iB8oIv9" : {
      "foodId" : "-LTHdfW1oNVz9iB8oIv9",
      "foodName" : "MEE",
      "foodType" : "Breakfast"
    },
    "-LTLwn3D3gkbKoLmOa-9" : {
      "foodId" : "-LTLwn3D3gkbKoLmOa-9",
      "foodName" : "Egg",
      "foodType" : "Breakfast"
    }

这是我成功检索数据的活动

package com.example.asus.mobilefoodpicker;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;

public class Activity2 extends AppCompatActivity {

private Button sbtn;
private Foodlist adapter;
DatabaseReference databasemobilefoodpicker;
ListView listViewFood;

List<Food> foodlists;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);
    databasemobilefoodpicker = 
FirebaseDatabase.getInstance().getReference("food");



databasemobilefoodpicker.addListenerForSingleValueEvent
(valueEventListener);
    Query query = FirebaseDatabase.getInstance().getReference("food")
            .orderByChild("foodType")
            .equalTo("Breakfast");

    query.addListenerForSingleValueEvent(valueEventListener);


    foodlists = new ArrayList<>();

    listViewFood = (ListView) findViewById(R.id.listViewFood);
    adapter = new Foodlist(Activity2.this, foodlists);
    listViewFood.setAdapter(adapter);







}


@Override
protected void onStart() {
    super.onStart();
    databasemobilefoodpicker.addValueEventListener(new ValueEventListener() 
 {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            foodlists.clear();

            for(DataSnapshot foodSnapshot : dataSnapshot.getChildren()){
                Food food = foodSnapshot.getValue(Food.class);

                foodlists.add(food);
            }

            Foodlist adapter = new Foodlist(Activity2.this, foodlists);
            listViewFood.setAdapter(adapter);
        }

        @Override
        public void onCancelled( DatabaseError databaseError) {

        }
    });
    listViewFood.setOnItemLongClickListener(new 
AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, 
int position, long id) {

            Food food = foodlists.get(position);

            showUpdateDialog(food.getFoodId(),food.getFoodName());
            return false;
        }
    });
}
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        foodlists.clear();
        if (dataSnapshot.exists()) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Food food = snapshot.getValue(Food.class);
                foodlists.add(food);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
};



private void showUpdateDialog(final String foodId, String foodName){

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

    LayoutInflater inflater = getLayoutInflater();

    final View dialogView = inflater.inflate(R.layout.update_dialog,null);

    dialogBuilder.setView(dialogView);

    final TextView textViewName = (TextView) 
dialogView.findViewById(R.id.textViewName);
    final EditText editTextName = (EditText) 
dialogView.findViewById(R.id.editTextName);
    final Button buttonUpdate = (Button) 
dialogView.findViewById(R.id.buttonUpdate);
    final Spinner spinnerfoodtype = (Spinner) 
dialogView.findViewById(R.id.spinnertype);
    final Button buttonDelete = (Button) 
dialogView.findViewById(R.id.buttonDelete);

    dialogBuilder.setTitle("Updating Food:  " + foodName);

    final AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

    buttonUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = editTextName.getText().toString().trim();
            String type = spinnerfoodtype.getSelectedItem().toString();

            if(TextUtils.isEmpty(name)){
                editTextName.setError("Food Name Required");
                return;
            }

            updateFood(foodId, name, type);

            alertDialog.dismiss();

        }
    });
    buttonDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteFood(foodId);
            alertDialog.dismiss();

        }
    });
}

private void deleteFood(String foodId){
    DatabaseReference drFood =  
FirebaseDatabase.getInstance().getReference("food").child(foodId);

    drFood.removeValue();

    Toast.makeText(this,"Food is deleted", Toast.LENGTH_LONG).show();
}

private boolean updateFood(String id, String name, String type){
    DatabaseReference databaseReference = 
FirebaseDatabase.getInstance().getReference("food").child(id);

    Food food = new Food(id, name, type);

    databaseReference.setValue(food);

    Toast.makeText(this,"Food Updated Successfully", 
Toast.LENGTH_LONG).show();

    return true;
}

}

未成功获取查询数据的活动

package com.example.asus.mobilefoodpicker;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class Activity4 extends AppCompatActivity {
private Button sbtn;
private Foodlist adapter;
DatabaseReference databasemobilefoodpicker;
ListView list;

List<Food> foodlists1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_4);

    databasemobilefoodpicker = 
FirebaseDatabase.getInstance().getReference("food");
    databasemobilefoodpicker.addListenerForSingleValueEvent
(valueEventListener);
    Query query4 = FirebaseDatabase.getInstance().getReference("foodType")
            .orderByChild("foodType")
            .equalTo("Dinner");

    query4.addListenerForSingleValueEvent(valueEventListener);


    foodlists1 = new ArrayList<>();

    list = (ListView) findViewById(R.id.list);
    adapter = new Foodlist(Activity4.this, foodlists1);
    list.setAdapter(adapter);


}
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        foodlists1.clear();
        if (dataSnapshot.exists()) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Food food = snapshot.getValue(Food.class);
                foodlists1.add(food);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
};
}

0 个答案:

没有答案