从firebase数据库中删除项目后如何更新列表视图?

时间:2017-05-12 11:26:39

标签: android firebase firebase-realtime-database

我希望在从firebase数据库删除项目(使用删除按钮)后更新列表视图,虽然firebase数据库项被删除并且onChildRemoved方法被调用,列表视图不会更新。我将不得不重新启动应用程序以查看更新的列表视图。我尝试了stackoverflow上的所有可用内容。

        package com.example.agasthya.travelbooking;

        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.ListView;
        import android.widget.ProgressBar;

        import com.google.firebase.database.ChildEventListener;
        import com.google.firebase.database.DataSnapshot;
        import com.google.firebase.database.DatabaseError;
        import com.google.firebase.database.DatabaseReference;
        import com.google.firebase.database.FirebaseDatabase;
        import com.google.firebase.database.Query;
        import com.google.firebase.database.ValueEventListener;

        import java.util.ArrayList;
        import java.util.List;




        public class MainActivity extends AppCompatActivity {
            ChildEventListener mchild;
            private ListView mMessageListView;
            private MessageAdapter mMessageAdapter;
            private ProgressBar mProgressBar;
            private FirebaseDatabase mFirebaseDatabase;

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

                mFirebaseDatabase=FirebaseDatabase.getInstance();
                mMessageListView = (ListView) findViewById(R.id.messageListView);

                mMessagesDataBaseReference=mFirebaseDatabase.getReference().child("travelbooking");
                 final List<Messages> friendlyMessages = new ArrayList<>();

                mMessageAdapter = new MessageAdapter( this, R.layout.list_item, friendlyMessages);
                mMessageListView.setAdapter(mMessageAdapter);

                mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
                mProgressBar.setVisibility(ProgressBar.INVISIBLE);



                mchild=new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        Messages friendlyMessage = dataSnapshot.getValue(Messages.class);
                        mMessageAdapter.add(friendlyMessage);
                    }

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

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {Messages friendlyMessage = dataSnapshot.getValue(Messages.class);
                        mMessageAdapter.remove(friendlyMessage);
                        mMessageAdapter.notifyDataSetChanged();
                    }

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

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                };



                Button i =(Button)findViewById(R.id.button2);
                        i.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                             DatabaseReference ref = mFirebaseDatabase.getReference();
                             Query applesQuery = mMessagesDataBaseReference;

                            applesQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {int i=0;
                                for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) {
                                    appleSnapshot.getRef().removeValue();i++;





        if(i==1)break;
                                }}

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });}});

                mMessagesDataBaseReference.addChildEventListener(mchild);

            }


                }

消息适配器类

            package com.example.agasthya.travelbooking;


            import android.app.Activity;
            import android.content.Context;
            import android.view.View;
            import android.view.ViewGroup;
            import android.widget.ArrayAdapter;
            import android.widget.TextView;

            import com.google.firebase.database.ChildEventListener;

            import java.util.List;

            public class MessageAdapter extends ArrayAdapter<Messages> {
                public MessageAdapter(ChildEventListener context, int resource, List<Messages> objects) {
                    super(context, resource, objects);
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    if (convertView == null) {
                        convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.list_item, parent, false);
                    }


                    TextView messageTextView = (TextView) convertView.findViewById(R.id.messageTextView);
                    TextView authorTextView = (TextView) convertView.findViewById(R.id.nameTextView);
                    TextView authorTextView2 = (TextView) convertView.findViewById(R.id.nameTextView2);


                    Messages message = getItem(position);

                        messageTextView.setText("From :"+message.getFrom()+"\n");

                    authorTextView.setText("To :"+message.getTo()+"\n");
                    authorTextView2.setText("Name :"+message.getName()+"\n");

                    return convertView;
                }
            }

1 个答案:

答案 0 :(得分:0)

    package com.example.agasthya.travelbooking;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.ProgressBar;

    import com.google.firebase.database.ChildEventListener;
    import com.google.firebase.database.DataSnapshot;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.Query;
    import com.google.firebase.database.ValueEventListener;

    import java.util.ArrayList;
    import java.util.List;




    public class MainActivity extends AppCompatActivity implements ChildEventListener {
        ChildEventListener mchild;
        private ListView mMessageListView;
        private MessageAdapter mMessageAdapter;
        private ProgressBar mProgressBar;
        private FirebaseDatabase mFirebaseDatabase;

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

            mFirebaseDatabase=FirebaseDatabase.getInstance();
            mMessageListView = (ListView) findViewById(R.id.messageListView);

            mMessagesDataBaseReference=mFirebaseDatabase.getReference().child("travelbooking");
             final List<Messages> friendlyMessages = new ArrayList<>();

            mMessageAdapter = new MessageAdapter( this, R.layout.list_item, friendlyMessages);
            mMessageListView.setAdapter(mMessageAdapter);

            mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
            mProgressBar.setVisibility(ProgressBar.INVISIBLE);



            mchild= (ChildEventListener) this;



            Button i =(Button)findViewById(R.id.button2);
                    i.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                         DatabaseReference ref = mFirebaseDatabase.getReference();
                         Query applesQuery = mMessagesDataBaseReference;

                        applesQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {int i=0;
                            for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) {
                                appleSnapshot.getRef().removeValue();i++;





    if(i==1)break;
                            }}

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });}});

            mMessagesDataBaseReference.addChildEventListener(mchild);

        }

               @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Messages friendlyMessage = dataSnapshot.getValue(Messages.class);
                    mMessageAdapter.add(friendlyMessage);
                }

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

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {Messages friendlyMessage = dataSnapshot.getValue(Messages.class);
                    mMessageAdapter.remove(friendlyMessage);
                    mMessageAdapter.notifyDataSetChanged();
                }

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

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
  }

试试这个