为什么我无法从应用程序中删除firebase记录?

时间:2022-09-03 23:23:17

I originally had this working perfectly - adding/updating/deleting records. However, after adding some extra functionality the deleting of records is no longer possible.

我最初完美地工作 - 添加/更新/删除记录。但是,在添加一些额外功能后,不再可能删除记录。

I have been reading through the code for quite a while trying to figure out where I might have changed something, but to no avail. Very frustrated.

我一直在阅读代码一段时间,试图弄清楚我可能会改变一些东西,但无济于事。非常沮丧。

At present, when I click the delete button in my application it shows the Toast message saying that the record has been deleted, but alas the record stays.

目前,当我单击我的应用程序中的删除按钮时,它会显示Toast消息,说明该记录已被删除,但记录保持不变。

Any help on this would be much appreciated.

任何有关这方面的帮助将非常感激。

PropertyActivity

PropertyActivity

    package com.example.xxx.myapplication;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
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.ValueEventListener;

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

public class PropertyActivity extends AppCompatActivity {

    public static final String PROPERTY_NAME = "propertyname";
    public static final String PROPERTY_ID = "propertyid";

    EditText editTextProperty;
    EditText editTextPostcode;
    EditText editTextBedrooms;
    EditText editTextBathrooms;
    Button buttonAddProperty;
    Spinner spinnerFuel;
    Spinner spinnerStatus;
    Spinner spinnerEPC;

    DatabaseReference databaseProperties;

    ListView listViewProperties;

    List<Property> propertyList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property);

        databaseProperties = FirebaseDatabase.getInstance().getReference("properties");

        editTextProperty = (EditText) findViewById(R.id.editTextProperty);
        editTextPostcode = (EditText) findViewById(R.id.editTextPostcode);
        editTextBedrooms = (EditText) findViewById(R.id.editTextBedrooms);
        editTextBathrooms = (EditText) findViewById(R.id.editTextBathrooms);
        buttonAddProperty = (Button) findViewById(R.id.buttonAddProperty);
        spinnerStatus = (Spinner) findViewById(R.id.spinnerStatus);
        spinnerEPC = (Spinner) findViewById(R.id.spinnerEPC);
        spinnerFuel = (Spinner) findViewById(R.id.spinnerFuel);

        listViewProperties = (ListView) findViewById(R.id.listViewProperties);

        propertyList = new ArrayList<>();

        buttonAddProperty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                addProperty();

            }
        });

        listViewProperties.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                Property property = propertyList.get(position);
                Intent intent = new Intent(getApplicationContext(), AddTenantsActivity.class);

                intent.putExtra(PROPERTY_ID, property.getPropertyId());
                intent.putExtra(PROPERTY_NAME, property.getPropertyAddress());

                startActivity(intent);
            }
        });

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

                Property property = propertyList.get(position);

                showUpdateDialog(property.getPropertyId(), property.getPropertyAddress(), property.getPropertyPostcode(), property.getPropertyBedrooms(), property.getPropertyBathrooms(), property.getPropertyStatus(), property.getPropertyEPC(), property.getPropertyFuel());
                return false;
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        databaseProperties.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                propertyList.clear();

                for(DataSnapshot propertySnapshot : dataSnapshot.getChildren()){
                    Property property = propertySnapshot.getValue(Property.class);

                    propertyList.add(property);

                }

                PropertyList adapter = new PropertyList (PropertyActivity.this, propertyList);
                listViewProperties.setAdapter(adapter);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void showUpdateDialog(final String id, String propertyAddress, String propertyPostcode, String propertyBedrooms, String propertyBathrooms, String propertyStatus, final String propertyId, String propertyName) {

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

        LayoutInflater inflater = getLayoutInflater();

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

        dialogBuilder.setView(dialogView);

        final EditText editTextName = (EditText) dialogView.findViewById(R.id.editTextName);
        final EditText editTextPostcode = (EditText) dialogView.findViewById(R.id.editTextPostcode);
        final EditText editTextBedrooms = (EditText) dialogView.findViewById(R.id.editTextBedrooms);
        final EditText editTextBathrooms = (EditText) dialogView.findViewById(R.id.editTextBathrooms);
        final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdate);
        final Spinner spinnerStatus = (Spinner) dialogView.findViewById(R.id.spinnerStatus);
        final Spinner spinnerEPC = (Spinner) dialogView.findViewById(R.id.spinnerEPC);
        final Spinner spinnerFuel = (Spinner) dialogView.findViewById(R.id.spinnerFuel);
        final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDelete);

        dialogBuilder.setTitle("Updating Property " + propertyName);

        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 postcode = editTextPostcode.getText().toString().trim();
                String bedrooms = editTextBedrooms.getText().toString().trim();
                String bathrooms = editTextBathrooms.getText().toString().trim();
                String status = spinnerStatus.getSelectedItem().toString();
                String epc = spinnerEPC.getSelectedItem().toString();
                String fuel = spinnerFuel.getSelectedItem().toString();

                if(TextUtils.isEmpty(name)){

                    editTextName.setError("Name Required");
                    return;
                }

                updateProperty(propertyId, status, postcode, bedrooms, bathrooms, id, name, epc, fuel);

                alertDialog.dismiss();
            }
        });

        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteProperty(propertyId);
            }
        });
    }

    private void deleteProperty(String propertyId) {
        DatabaseReference drProperty = FirebaseDatabase.getInstance().getReference("properties").child(propertyId);
        DatabaseReference drTenants = FirebaseDatabase.getInstance().getReference("tenants").child(propertyId);

        drProperty.removeValue();
        drTenants.removeValue();

        Toast.makeText(this, "Property has been deleted", Toast.LENGTH_LONG).show();
    }

    private boolean updateProperty(String propertyId, String status, String postcode, String bedrooms, String bathrooms, String id, String name, String epc, String fuel){

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("properties").child(id);
        Property property = new Property (id, name, postcode, bedrooms, bathrooms, status, epc, fuel);

        databaseReference.setValue(property);

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

        return true;
    }

    private void addProperty(){

        String address = editTextProperty.getText().toString().trim();
        String postcode = editTextPostcode.getText().toString().trim();
        String bedrooms = editTextBedrooms.getText().toString().trim();
        String bathrooms = editTextBathrooms.getText().toString().trim();
        String status = spinnerStatus.getSelectedItem().toString();
        String epc = spinnerEPC.getSelectedItem().toString();
        String fuel = spinnerFuel.getSelectedItem().toString();

        if(!TextUtils.isEmpty(address)){

            String id = databaseProperties.push().getKey();

            Property property = new Property (id, address, postcode, bedrooms, bathrooms, status, epc, fuel);

            databaseProperties.child(id).setValue(property);

            Toast.makeText(this, "Property Added", Toast.LENGTH_LONG).show();

        } else {

            Toast.makeText(this, "You must enter a property", Toast.LENGTH_LONG).show();
        }

    }
}

1 个解决方案

#1


0  

It looks like the arguments passed to showUpdateDialog() are mismatched. The method accepts eight arguments. The first six are not used (?) and the last two are propertyId and propertyName. When the method is called from onItemLongClick(), the values for propertyId and propertyName are property.getPropertyEPC(), property.getPropertyFuel().

看起来传递给showUpdateDialog()的参数不匹配。该方法接受八个参数。前六个不使用(?),后两个是propertyId和propertyName。从onItemLongClick()调用该方法时,propertyId和propertyName的值为property.getPropertyEPC(),property.getPropertyFuel()。

I suspect this results in a bad value for propertyId, which is passed to deleteProperty() and used to create the database reference for the location to be removed.

我怀疑这会导致propertyId的值不正确,它会传递给deleteProperty()并用于为要删除的位置创建数据库引用。

When your code isn't working and the problem cannot be found by inspection, it's always a good idea to add Log statements to trace the execution flow and output the values of relevant variables. The output can be viewed in logcat. It's also good practice to include a CompletionListener on your database operations to get an indication of success or failure. For example:

当您的代码不能正常工作且无法通过检查发现问题时,最好添加Log语句来跟踪执行流并输出相关变量的值。可以在logcat中查看输出。在数据库操作中包含CompletionListener以获得成功或失败的指示也是一种很好的做法。例如:

private void deleteProperty(String propertyId) {
    Log.d("PropertyActivity", "deleteProperty: propertyId=" + propertyId);

    DatabaseReference drProperty = FirebaseDatabase.getInstance().getReference("properties").child(propertyId);
    DatabaseReference drTenants = FirebaseDatabase.getInstance().getReference("tenants").child(propertyId);

    drProperty.removeValue(new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            if (databaseError == null) {
                Log.d("PropertyActivity", "Property remove complete");
            } else {
                throw databaseError.toException();
            }
        }
    });

    drTenants.removeValue();

    Toast.makeText(this, "Property has been deleted", Toast.LENGTH_LONG).show();
}

#1


0  

It looks like the arguments passed to showUpdateDialog() are mismatched. The method accepts eight arguments. The first six are not used (?) and the last two are propertyId and propertyName. When the method is called from onItemLongClick(), the values for propertyId and propertyName are property.getPropertyEPC(), property.getPropertyFuel().

看起来传递给showUpdateDialog()的参数不匹配。该方法接受八个参数。前六个不使用(?),后两个是propertyId和propertyName。从onItemLongClick()调用该方法时,propertyId和propertyName的值为property.getPropertyEPC(),property.getPropertyFuel()。

I suspect this results in a bad value for propertyId, which is passed to deleteProperty() and used to create the database reference for the location to be removed.

我怀疑这会导致propertyId的值不正确,它会传递给deleteProperty()并用于为要删除的位置创建数据库引用。

When your code isn't working and the problem cannot be found by inspection, it's always a good idea to add Log statements to trace the execution flow and output the values of relevant variables. The output can be viewed in logcat. It's also good practice to include a CompletionListener on your database operations to get an indication of success or failure. For example:

当您的代码不能正常工作且无法通过检查发现问题时,最好添加Log语句来跟踪执行流并输出相关变量的值。可以在logcat中查看输出。在数据库操作中包含CompletionListener以获得成功或失败的指示也是一种很好的做法。例如:

private void deleteProperty(String propertyId) {
    Log.d("PropertyActivity", "deleteProperty: propertyId=" + propertyId);

    DatabaseReference drProperty = FirebaseDatabase.getInstance().getReference("properties").child(propertyId);
    DatabaseReference drTenants = FirebaseDatabase.getInstance().getReference("tenants").child(propertyId);

    drProperty.removeValue(new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            if (databaseError == null) {
                Log.d("PropertyActivity", "Property remove complete");
            } else {
                throw databaseError.toException();
            }
        }
    });

    drTenants.removeValue();

    Toast.makeText(this, "Property has been deleted", Toast.LENGTH_LONG).show();
}