Reputation: 49
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.
Any help on this would be much appreciated.
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();
}
}
}
Upvotes: 0
Views: 150
Reputation: 38319
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()
.
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.
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:
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();
}
Upvotes: 1