Fadil Fadil - 8 days ago 5
Android Question

Android ListView Deletion when data is deleted at firebase

So I am building a simple todo app using android SDK and firebase as the backend, and everything seems to work as for the data gets added to the my firebase storage when I add any from the mobile, and the data gets deleted as well, but the data only gets deleted from the firebase storage, it doesn't get deleted from the mobile activity/screen, so how do I delete the data from the activity as well.

Here is the MainActivity.java

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
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 com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

// Get ListView object from xml
final ListView listView = (ListView)findViewById(R.id.listView);

// Create a new Adapter
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1);

// Assign adapter to ListView
listView.setAdapter(adapter);

// Use Firebase to populate the list.
Firebase.setAndroidContext(this);

new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems")
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
adapter.add((String)dataSnapshot.child("text").getValue());
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
adapter.remove((String)dataSnapshot.child("text").getValue());
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

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

}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});

// Add items via the Button and EditText at the bottom of the window.
final EditText text = (EditText)findViewById(R.id.todoText);
final Button btn = (Button)findViewById(R.id.addButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems")
.push()
.child("text")
.setValue(text.getText().toString());
}
});

// Delete items when clicked
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MainActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.notifyDataSetChanged();
}
});
adb.show();
new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems")
.orderByChild("text")
.equalTo((String) listView.getItemAtPosition(position))
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChildren()){
DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
firstChild.getRef().removeValue();
}
}

@Override
public void onCancelled(FirebaseError firebaseError) {}
});
}
});

}
}


the MY-FIREBASE-APP == already changed to my firebase app name, I just put a placeholder there.

Need help :)

Answer

Edit your MainAcitvity.java like below i hope it works. For Details understanding have a look at the below link. https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio

 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 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 com.firebase.client.ChildEventListener;
 import com.firebase.client.DataSnapshot;
 import com.firebase.client.Firebase;
 import com.firebase.client.FirebaseError;
 import com.firebase.client.ValueEventListener;

 public class MainActivity extends AppCompatActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);

         // Get ListView object from xml
         final ListView listView = (ListView)findViewById(R.id.listView);

         // Create a new Adapter
         final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1);

         // Assign adapter to ListView
         listView.setAdapter(adapter);

         // Use Firebase to populate the list.
         Firebase.setAndroidContext(this);

         new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems")
            .addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                         adapter.add((String)dataSnapshot.child("text").getValue());
                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                         adapter.remove((String)dataSnapshot.child("text").getValue());
                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {
adapter.remove((String)dataSnapshot.child("text").getValue());
                }

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

                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });

    // Add items via the Button and EditText at the bottom of the window.
    final EditText text = (EditText)findViewById(R.id.todoText);
    final Button btn = (Button)findViewById(R.id.addButton);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems")
                    .push()
                    .child("text")
                    .setValue(text.getText().toString());
        }
    });

    // Delete items when clicked
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            AlertDialog.Builder adb=new AlertDialog.Builder(MainActivity.this);
            adb.setTitle("Delete?");
            adb.setMessage("Are you sure you want to delete " + position);
            adb.setNegativeButton("Cancel", null);
            adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                     new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems")
                    .orderByChild("text")
                    .equalTo((String) listView.getItemAtPosition(position))
                    .addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if(dataSnapshot.hasChildren()){
                                DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
                                firstChild.getRef().removeValue();
                            }
                        }

                        @Override
                        public void onCancelled(FirebaseError firebaseError) {}
                    });
                    adapter.notifyDataSetChanged();
                }
            });
            adb.show();

        }
    });

    }
 }