Android - Update and delete data in Firebase database
<p>I'm trying to update and delete data in Firebase database.</p>

<p><code>SectionDetails</code> model</p>

<pre><code>public class SectionDetails {
    private String sectionCode;
    private String sectionSeats;
    private String sectionKey;

public SectionDetails() {
}

public SectionDetails(String sectionCode, String sectionSeats) {
    this.sectionCode = sectionCode;
    this.sectionSeats = sectionSeats;
}

@Exclude
public String getSectionKey() {
    return sectionKey;
}

public String getSectionCode() {
    return sectionCode;
}

public String getSectionSeats() {
    return sectionSeats;
}
}
</code></pre>

<p><code>FirebaseHelper</code> class</p>

<pre><code>public class FirebaseHelper {
        DatabaseReference db;
        ArrayList&lt;SectionDetails&gt; sectionDetailsArrayList = new ArrayList&lt;&gt;();

public FirebaseHelper(DatabaseReference db) {
    this.db = db;
}

private void fetchData(DataSnapshot dataSnapshot) {

    SectionDetails sectionDetails = dataSnapshot.getValue(SectionDetails.class);
    sectionDetailsArrayList.add(sectionDetails);

    adapter.notifyDataSetChanged();
}

public ArrayList&lt;SectionDetails&gt; retrieve() {
    db.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            fetchData(dataSnapshot);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            fetchData(dataSnapshot);
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return sectionDetailsArrayList;
}
}
</code></pre>

<p><code>CustomAdapter</code> class</p>

<pre><code>public class CustomAdapter extends BaseAdapter {
        DatabaseReference updateRef;
        String key;
        Context c;
        ArrayList&lt;SectionDetails&gt; sectionDetailsArrayList;

public CustomAdapter(Context c, ArrayList&lt;SectionDetails&gt; sectionDetailsArrayList) {
    this.c = c;
    this.sectionDetailsArrayList = sectionDetailsArrayList;
}

@Override
public int getCount() {
    return sectionDetailsArrayList.size();
}

@Override
public Object getItem(int position) {
    return sectionDetailsArrayList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final SectionDetails sd = (SectionDetails) this.getItem(position);

    updateRef = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_COURSES).child("sections");

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Dialog d = new Dialog(CustomAdapter.this);
            d.setContentView(R.layout.section_custom_dialog);

            Button btnUpdate = (Button) d.findViewById(R.id.btnUpdate);
            Button btnDelete = (Button) d.findViewById(R.id.btnDelete);

            btnUpdate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final String code = "1B";
                        final String seats = "20";

                        updateRef.addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {
                                            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                                SectionDetails updateSD = snapshot.getValue(SectionDetails.class);
                                                if (sd.getSectionCode().equals(updateSD.getSectionCode())) {
                                                    key = snapshot.getKey().toString();
                                                }
                                            }

                                        SectionDetails newSD = new SectionDetails(code, seats);
                                        updateRef.child(key).setValue(newSD);
                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {

                                        }
                                    });

                    }
            });

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

                                updateRef.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                            SectionDetails deleteSD = snapshot.getValue(SectionDetails.class);
                                            if (sd.getSectionCode().equals(deleteSD.getSectionCode())) {
                                                updateSectionRef.child(snapshot.getKey().toString()).removeValue();
                                                break;
                                            }
                                        }
                                        adapter.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });
                    }
                });

            d.show();
        }
    });


    return convertView;
}
}
</code></pre>

<p><code>MainActivity</code> class</p>

<pre><code>public class MainActivity extends AppCompatActivity {

DatabaseReference mRef;
FirebaseHelper helper;
CustomAdapter adapter;
ListView lvSectionsListOne;

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

lvSectionsListOne = (ListView) findViewById(R.id.lvSectionsList);

mRef = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_COURSES).child("sections");
helper = new FirebaseHelper(mRef);

adapter = new CustomAdapter(this, helper.retrieve());
lvSectionsListOne.setAdapter(adapter);
}
}
</code></pre>

<p>The data is deleted from database as expected, but the data that gets deleted remains inside the <code>listview</code>. I added <code>adapter.notifyDataSetChanged()</code> but still the <code>listview</code> is not updating.</p>

<p>The data is also updated as expected, but when <code>update</code> button is clicked, the data is updated infinitely. I can see the <code>listview</code> as well as the database keep on appending the data, and can only be stopped by closing the app.</p>

<p>I have tried to move <code>SectionDetails newSD = new SectionDetails(code, seats)</code> and <code>updateRef.child(key).setValue(newSD)</code> to outside <code>for</code> loop but the data doesn't get updated because the key is not passed to the path outside the <code>for</code> loop.</p>