java - Android Firebase数据库自动刷新更新2

标签 java android firebase firebase-realtime-database

我已经实现了 Firebase 数据库,并且可以成功向其中添加项目,然后将它们显示在 RecyclerView 上。我还成功地实现了数据库子级的删除,但我需要重新启动 Activity 才能看到手机屏幕上的更改。例如:当我在列表项上按“删除”时,它会立即从数据库中消失,但我需要重新启动 Activity 才能看到更改。这是我的代码 fragment :

 private void attachDatabaseReadListener() {

    queryRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
            locationCurrent.setRefKey(dataSnapshot.getKey());
            mLocationAdapter.add(locationCurrent);

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            locationCurrent = dataSnapshot.getValue(LocationCurrent.class);


        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

我相信我应该致力于 onChildRemoved 但不知道从哪里开始。我的主要想法是使用 for 循环重新填充 recyclerview 但我从 datasnapshot 获得的 locationCurrent 对象为 null。

有什么想法我应该从哪里开始寻找解决方案吗?我还考虑过在查询上运行 addValueEventListener 方法,但遇到了一个问题:我获得了单个子项的多个副本

更新 引用这里的一些评论是我的适配器

public class LocationAdapter extends ArrayAdapter<LocationCurrent> {



public LocationAdapter(Context context, int resource, List<LocationCurrent> objects) {
    super(context, resource, objects);
}

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

    LocationCurrent currentLocation = getItem(position);

    TextView nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);

    TextView descriptionTextView = (TextView) convertView.findViewById(R.id.descriptionTextView);


    nameTextView.setText(currentLocation.getName());
    descriptionTextView.setText(currentLocation.getDescription());



    return convertView;
}

}

顺便说一句 - 我在 locationCurrent 类中使用的 Ref Key 是 transient 变量,因此在数据库中不可见。

更新2

经过一整天的工作,我仍然没能在删除该项目后立即将其从适配器上删除。相反 - 我想出了一个临时解决方案 - 我在我的 onChildRemoved 中添加了一个 recreate() 方法,它完成了它的工作。 (这不是一个好的做法,但仍然是一些东西)

最佳答案

基本上,这就是我的删除方法。

            /**
             * This removes the CardView from the RecyclerView, allowing us to create a delete request
             *
             * http://stackoverflow.com/questions/27293960/swipe-to-dismiss-for-recyclerview/30601554#30601554
             *
             * @param viewHolder
             * @param direction
             */
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                // First find the position of the card
                // https://www.learn2crack.com/2016/02/custom-swipe-recyclerview.html
                int position = viewHolder.getAdapterPosition();

                // Connect to the database, identify which card was swiped via the RecyclerViewAdapter
                DatabaseReference currRef = adapter.getRef(position);

                // Get it out of the DB
                currRef.removeValue();
            }

上面的以下代码应该可以让您大致了解删除过程。您需要检索要删除的元素的索引。在您的上下文中,您的位置键会以某种方式绑定(bind)。

仅供引用,这是基于 FirebaseRecyclerAdapter 的 populateViewHolder。

                    /**
                     * Populating the RecyclerView..
                     *
                     * @param viewHolder
                     *
                     *
                     * @param task
                     *
                     *
                     * @param position
                     * With the use of position, we can obtain the key of from the FirebaseDatabase
                     *
                     * http://stackoverflow.com/questions/37568703/how-to-get-keys-and-values-using-firebaselistadapter
                     */
                    @Override
                    protected void populateViewHolder(TaskViewHolder viewHolder, Tasks task, int position) {
                        // Get the key of the Tasks object
                        //String currentKey = getRef(position).push().getKey();
                        //final String currentKey = getRef(position).toString(); // This returns the object URL from Firebase
                        final String currentKey = getRef(position).getKey();
                        Log.d(TAG, currentKey.toString());
                        Log.d(TAG, "Image: " + task.getImageUrl());

                        // Perform some DateTime formatting from the ISO8601 format

                        // Basically we need to attach the task to the viewHolder so that
                        // the cards can instantiate their view properly
                        viewHolder.setTaskName(task.getTaskName());
                        viewHolder.setTaskDesc(task.getTaskDescription());
                        viewHolder.setTaskDate(task.getTaskDeadline());
                        viewHolder.setTaskImage(task.getImageUrl());

                        final Intent updateView = new Intent(getActivity(), UpdateTaskActivity.class);

                        // Implement Serializable on the Tasks object,
                        // Push the object directly via updateView.putExtra
                        // That way we can have everything we need in the object.

                        //updateView.putExtra("TaskName", task.getTaskName());
                        //updateView.putExtra("TaskDesc", task.getTaskDescription());
                        updateView.putExtra("TaskObject", task);
                        updateView.putExtra("Key", currentKey);

                        viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                /**
                                 * How to provide a foundation to animate cards
                                 *
                                 * http://stackoverflow.com/questions/27300441/how-do-i-expand-cardviews-to-show-more-detail-like-google-keep-cards
                                 */
                                //Toast.makeText(getActivity(), currentKey, Toast.LENGTH_LONG).show(); // Test Line to Showcase the Key.

                                ActivityOptionsCompat options =
                                        ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),
                                                v,   // The view which starts the transition
                                                getString(R.string.transition_taskcard)    // The transitionName of the view we’re transitioning to
                                        );

                                ActivityCompat.startActivity(getActivity(), updateView, options.toBundle());
                            }
                        });
                    }

仅显示此示例是为了让您知道您的 View 和删除代码都必须是动态的。

如果您要使用动态 View ,请参阅此处的文档 https://github.com/firebase/emberfire

更新由于您刚刚添加了适配器,因此如果您愿意,也可以在 getView 中使用 onClickListener 来执行任何删除操作。

关于java - Android Firebase数据库自动刷新更新2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41772002/

相关文章:

android - 删除所有 recyclerview 项目后 TextView 将不会显示

java - android中的NotificationCompat.MediaStyle()中只有上一个按钮起作用

android - 在应用程序关闭时执行挂起的写入

Firebase 测试数据

swift - 我想从安装了 cocoapods 的 Xcode 项目中删除 firebase

java - 如何从 onDataChange 函数设置全局变量

java - Glassfish 记录日期和夏令时?

java - JAXB 自定义和列表<Object>

java - 在Java中,如何找到自上次鼠标移动以来的时间?

java - 浏览器关闭时 Servlet 长处理取消