Glen Micallef
Glen Micallef

Reputation: 87

Deleting firebase entry node Android Java

I added a list of countries to firebase from a listview and now im trying to delete nodes seperately by list view item click.

That list is now shown in a listview which i want that when i click on a particular country it gets removed. So far i have done:

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

            String Country =(listView.getItemAtPosition(position).toString());
            showPopUp(Country);
        }
    });

That got the Country that i press on. In the popup menu method i have:

public void showPopUp(final String cntry) {

    PopupMenu popupMenu = new PopupMenu(this, (RelativeLayout) findViewById(R.id.id));
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {

                case R.id.Delete:



                    // Delete from Firebase

                    dref.child(cntry).removeValue();

                    Toast.makeText(getApplicationContext(), cntry,
                            Toast.LENGTH_LONG).show();
                    return true;

                case R.id.Goback:

                    return true;
                default:
                    return false;
            }


        }

    });

Although i think i need to remove the key not the child from firebase in my case. How should i implement this please?

Note: I added the countries from API to list view by:

 DatabaseReference dref;
    ListView listView;
    ArrayList<String> list = new ArrayList<>();
    ArrayAdapter<String> adapter;


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

        listView = (ListView) findViewById(R.id.lstViewFavCountries);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        dref = FirebaseDatabase.getInstance().getReference();
        dref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {


                for(DataSnapshot dsp : dataSnapshot.getChildren()){
                    String value = dataSnapshot.getValue().toString();
                    list.add(String.valueOf(dsp.getValue()));
                }

                adapter.notifyDataSetChanged();
            }

https://gyazo.com/705654bde00d245bed57bfd45f1a2f67 the link of database

Upvotes: 0

Views: 108

Answers (1)

Wilik
Wilik

Reputation: 7720

You need to save the key somewhere, call dataSnapshot.getKey() to get the key.

First of all, create a new list for the keys

ArrayList<String> listKey = new ArrayList<>();

Then add the key to the list

public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    for(DataSnapshot dsp : dataSnapshot.getChildren()){
        String value = dataSnapshot.getValue().toString();
        list.add(String.valueOf(dsp.getValue()));
        listKey.add(dsp.getKey());
    }
    adapter.notifyDataSetChanged();
}

Finally, in your onItemClick method, get the key from the list based on the position then pass the string to the deletion method

public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
    String CountryKey = listKey.get(position);
    showPopUp(CountryKey);
}

Then to delete the value

dref.child("Countries").child(cntry).removeValue();

Upvotes: 1

Related Questions