• 9
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm new to Android Studio so this may be easy for some of you.

I have a spinner that reads Names of Visitors from Firebase Database.

Once the Name is chosen and a button is clicked I want that child to be removed from the Database.

This is my code for Spinner in onCreate:

databaseVisitors = FirebaseDatabase.getInstance().getReference();
databaseVisitors.child("visitorsToCO").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot datasnapshot) {
        final List<String> visitorsToCO = new ArrayList<String>();

        for (DataSnapshot areaSnapshot: datasnapshot.getChildren()) {
            String visitorName = areaSnapshot.child("visitorName").getValue(String.class);
            if (visitorName!=null){
                visitorsToCO.add(visitorName);
            }
        }

        Spinner spinnerV = (Spinner) findViewById(R.id.visitorName);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(CheckOutActivity.this, android.R.layout.simple_spinner_item, visitorsToCO);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerV.setAdapter(arrayAdapter);

    }

And this is my code for the button:

btnCheckOut2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addVisitorCheckOut();
            deleteVisitorToCO();
            openMainActivity();
        }
    });

public void deleteVisitorToCO()
{
    databaseVisitors = FirebaseDatabase.getInstance().getReference("visitorsToCO");

    final String name = visitorName.getSelectedItem().toString();

    Query query = databaseVisitors.child("visitorsToCO").orderByChild("visitorName").equalTo(name);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot itemSnapshot : dataSnapshot.getChildren()) {
                itemSnapshot.getRef().removeValue();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

This is my Firebase Database

databaseVisitors = FirebaseDatabase.getInstance().getReference();
databaseVisitors.child("visitorsToCO").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot datasnapshot) {

        for (DataSnapshot areaSnapshot: datasnapshot.getChildren()) {
            if(areaSnapshot.child(visitorName).getValue().toString().equals(spinnerV.getSelectedItem().toString()))
            {
                areaSnapShot.setValue(null);
            }

        }


    }
  • 1
Reply Report
      • 1
    • Thanks for that, just a small change was needed by changing areaSnapShot.setValue(null); to areaSnapshot.getRef().removeValue();. Greatly appreciated, thanks again