Android ListView在firebase删除数据时删除

时间:2015-10-27 09:32:24

标签: android listview firebase

所以我正在构建一个使用android SDK和firebase作为后端的简单todo应用程序,当我从移动设备添加任何数据时,所有内容似乎都工作,因为数据被添加到我的firebase存储中,并且数据被删除为好吧,但数据只会从firebase存储中删除,不会从移动活动/屏幕中删除,所以如何从活动中删除数据。

这是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) {}
                        });
            }
        });

    }
}

MY-FIREBASE-APP ==已经更改为我的firebase应用程序名称,我只是在那里放了一个占位符。

需要帮助:)

1 个答案:

答案 0 :(得分:2)

编辑您的MainAcitvity.java,如下所示,我希望它有效。 有关详细信息,请查看以下链接。 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();

        }
    });

    }
 }