Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Firebase数据库自动刷新更新2_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java Android Firebase数据库自动刷新更新2

Java Android Firebase数据库自动刷新更新2,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我已经实现了Firebase数据库,可以成功地向其中添加项目,然后在RecyclerView上显示它们。我还成功地删除了数据库的一个子项,但我需要重新启动活动才能在手机屏幕上看到更改。例如:当我在列表项上按Delete键时,它会立即从数据库中消失,但我需要重新启动活动以查看更改。以下是我的代码片段: private void attachDatabaseReadListener() { queryRef.addChildEventListener(new ChildEventList

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

 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) {

        }
    });

}
我相信我应该在孩子搬家后继续工作,但不知道从哪里开始。我的主要想法是使用for循环重新填充
recyclerview
,但我从datasnapshot获得的locationCurrent对象为空

我应该从哪里开始寻找解决方案?我还考虑在查询中运行
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;
}
公共类LocationAdapter扩展了ArrayAdapter{
公共位置适配器(上下文、int资源、列表对象){
超级(上下文、资源、对象);
}
@非空
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=((活动)getContext()).GetLayoutFlater().inflate(R.layout.item_位置,父项,false);
}
LocationCurrentLocation=getItem(位置);
TextView名称TextView=(TextView)convertView.findViewById(R.id.nameTextView);
TextView descriptionTextView=(TextView)convertView.findViewById(R.id.descriptionTextView);
nameTextView.setText(currentLocation.getName());
descriptionTextView.setText(currentLocation.getDescription());
返回视图;
}
}

顺便说一下,locationCurrent类中使用的my Ref键是瞬态变量,因此在数据库中不可见

更新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();
            }
下面的代码应该可以让您大致了解删除过程。您需要检索要删除的元素的索引。在您的上下文中,您的位置键可能会以某种方式绑定

仅供参考,这是基于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());
                            }
                        });
                    }
显示这个示例是为了让您知道视图和删除代码都必须是动态的

如果您使用的是动态视图,请参阅此处的文档


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

您需要在
mLocationAdapter
中找到
dataSnapshot
的位置,然后将其从适配器中删除。如何做到这一点取决于您的适配器实现。但是,考虑到您已经将快照的密钥放入
LocationCurrent
对象中,它应该是一个相当简单的查找。谢谢您,我不认为我必须这样做。我说的一切都是在
onChildRemoved
中完成的,并且
DataSnapshot
在该方法中是我删除的项目的快照,对吗。这确实是解决问题的方法。请记住,有一些现成的适配器已经完成了所有这一切。你可以用它们,或者至少用它们来(复制/粘贴)灵感。谢谢。我只是好奇为什么简单
mLocationAdapter.remove(locationCurrent)不工作?因为snapshot返回一个我正在删除的对象,并且在这行代码中,我选择了一个locationCurrent对象,该对象将从适配器中删除。@martynaskuc不确定原因,但还是最好直接按其键删除该位置。这样,你就知道你在firebase数据库中引用了完全相同的东西。好吧,我知道你做了什么。但是如何在
@Override public void onChildRemoved(DataSnapshot DataSnapshot){}
中获取要删除的项的位置?我可以得到Firebase数据库生成的密钥。但基本上就是这样