当前位置: 首页 > 知识库问答 >
问题:

FirebaseRecyclerAdapter对我不起作用

伍嘉
2023-03-14

我需要父键和它的子键来表示RecycerView中的纬度和经度列表。我想我需要对FirebaseRecyclerAdapter中的PopulateViewholder中的LatlngsModel项做一些事情,但是不管做什么,我都做不对。

我目前循环所有的父键和它的子键作为它的许多父键,并将它添加到列表中...

JSON结构:

{
  "users" : {
    "0057242b-81e2-4f97-bca7-b671212614ba" : {
      "email" : "kalle@hotmail.se",
      "waypoints" : {
        "-KH9UAPH5NmLJExaUa5g" : {
          "-KH9UAPH5NmLJExaS2s" : {
            "latitude" : 111,
            "longitude" : 111.1
          }
        },
        "-KHB1VjqUdO90vxj9XCh" : {
          "-KHB1VjqUdO90vxj9XCi" : {
            "latitude" : 222.1,
            "longitude" : 222.11
          },
          "-KHB1ZykbwgXM9sPNie9" : {
            "latitude" : 222.2,
            "longitude" : 222.22
          }
        }
      }
    },
@JsonIgnoreProperties(ignoreUnknown=true)
public class User{

    @JsonProperty("email")
    String email;

    MyWaypoint waypoints;

    public User(){}

    public User(String email){
        this.email = email;
    }

    public String getEmail(){return this.email; }
    public void setEmail(String _email){this.email = _email;}


    public MyWaypoint getWaypoints(){return waypoints;}
        public void setWaypoints(MyWaypoint points){ this.waypoints = points;}

    }

纬度和经度POJO。

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyWaypoint {

    private double latitude;
    private double longitude;

    public MyWaypoint() {
    }
    public MyWaypoint(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }
    public double getLongitude() {
        return longitude;
    }
}

我的活动

public class MapListActivityRealBack2 extends AppCompatActivity {

    private String LOG_TAG = "LOG_TAG";
    private Firebase mRef;
    private Firebase userRef;
    private String mUserId;

    FirebaseRecyclerAdapter<User, LatLngViewHolderBack2> mAdapter;


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


        mRef = new Firebase(Constants.FIREBASE_URL);
        if (mRef.getAuth() == null) {
            loadLoginView();
        }


        try {
            mUserId = mRef.getAuth().getUid();
        } catch (Exception e) {
            loadLoginView();
        }


        final RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
        mRecyclerView.setHasFixedSize(false);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setReverseLayout(false);
        mRecyclerView.setLayoutManager(manager);


        // https://todoapprj.firebaseio.com/users/1a96a633-7e67-41b8-9aa7-c70d4b7eb59c
        final String userUrl = Constants.FIREBASE_URL + "/users/" + mUserId;
        userRef = new Firebase(userUrl);


        mAdapter = new FirebaseRecyclerAdapter<User, LatLngViewHolderBack2>(User.class, R.layout.list_item, LatLngViewHolderBack2.class, userRef) {
            @Override
            protected void populateViewHolder(final LatLngViewHolderBack2 latLngViewHolder, User item, final int i) {




                userRef.addValueEventListener(new ValueEventListener() {
                    List<MyWaypoint> userWayPointsList = new ArrayList<MyWaypoint>();

                    @Override
                    public void onDataChange(DataSnapshot wayPointsDataSnapshot) {
                        if(wayPointsDataSnapshot.getChildrenCount() > 0){
                            for (DataSnapshot wayPointsSnapshotChild : wayPointsDataSnapshot.getChildren()){
                                Log.i("FireBaseTester", "For-Loop :: wayPointsSnapshotChild.getValue() : "+wayPointsSnapshotChild.getValue());
                                if(wayPointsSnapshotChild.getChildrenCount()>0){

                                    for (DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()){
                                        //this is where we get the Lat and Lon
                                        double latitude = Double.parseDouble(wayPointsChild.child("latitude").getValue().toString());
                                        double longitude = Double.parseDouble(wayPointsChild.child("longitude").getValue().toString());
                                        userWayPointsList.add(new MyWaypoint( latitude,  longitude));
                                        Log.i("FireBaseTester","latitude = "+latitude+" , longitude = "+longitude);
                                    }
                                }

                            }
                        }
                        //here you can assign the points to the user
                        Log.i("FireBaseTester","There are "+userWayPointsList.size()+ " Points for User");
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {
                        Log.e("FireBaseTester", "onCancelled - wayPointRef Error is " + firebaseError.getMessage());
                    }
                });
            }
        };

        mRecyclerView.setAdapter(mAdapter);

    }



    private void loadLoginView() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
}

致命异常:main process:com.example.rasmusjosefsson.rjcar,pid:19134 com.firebase.client.firebaseException:在com.firebase.ui.firebaserecycleradapter.parsesnapshot(firebaseecycleradapter.java:161)com.firebase.ui.firebaseecycleradapter.getvalue(datasnapshot.java:185)在port.v7.widget.recyclerview$adapter.onbindviewholder(recycerview.java:5471)在Android.support.v7.widget.recyclerview$adapter.bindviewholder(recycerview.java:5504)在Android.support.v7.widget.recyclerview$recycer.getviewforposition(recycerview.java:4741)在Android.support.v7.widget.recyclerview.java:4741)在.LinearLayoutManager.LayoutcAndroid.support.v7.widget.linearlayoutmanager.java:1390)在Android.support.v7.widget.linearlayoutmanager.fill(Linearlayoutmanager.java:1353)在Android.support.v7.widget.linearlayoutmanager.onlayoutchildren(Linearlayoutmanager.java:1374)在Android.support.v7.widget.linearlayoutmanager.onlayoutchildren(view.layout(view.java:16630)在Android.view.viewgroup.layout(viewgroup.java:5437)在Android.support.design.widget.headerscrollingviewBehavior.layoutchild(headerscrollingviewBehavior.java:122)在Android.support.design.widget.viewoffsetBehavior.java:122)在16630)在Android.view.viewgroup.layout(viewgroup.java:5437)在Android.widget.framelayout.layoutchildren(framelayout.java:336)在Android.widget.framelayout.onlayout(framelayout.java:273)在Android.view.view.viewgroup.layout(view.java:16630)在Android.view.view.viewgroup.layout(view.java:5437)在:1495)在Android.view.view.layout(view.java:16630)在Android.view.viewgroup.layout(viewgroup.java:5437)在Android.widget.framelayout.layoutchildren(framelayout.java:336)在Android.widget.framelayout.onlayout(framelayout.java:273)在Android.view.viewgroup.layout(view.java:16630)在Android.view.viewgroup.layout(在Android.widget.linearlayout.onLayout(LinearLayout.java:1495)在Android.view.view.layout(view.java:16630)在Android.view.viewgroup.layout(viewgroup.java:5437)在Android.widget.framelayout.layoutchildren(framelayout.java:336)在Android.widget.framelayout.onlayout(framelayout.java:273)在com.Android.internal.policy.phoneWindow$decorview.java:2678)在pl.java:2171)在Android.view.viewrootimpl.performtraversals(viewrootimpl.java:1931)在Android.view.viewrootimpl.doTraversal(viewrootimpl.java:1107)在Android.view.viewrootimpl.java:1107),在Android.view.viewrootimpl.run(viewrootimpl.java:6013),在Android.view.choreographer$callbackrecord.run(choreographer.java:858),在$FrameDisplayEventReceiver.run(choreographer.java:844)在Android上。OS.H

共有1个答案

武彭薄
2023-03-14

更新后的答案将Firebase URL更改为最终字符串userUrl=constants.firebase_url+“/users”(去掉URL的其余部分)。我在这段新代码中所做的是遍历数据,并为每个lat-lon对创建MyWayPoint实例。我在代码中指出了您可以从哪里开始使用MyWayPoint来执行您想要的任何操作:

 userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.i("FireBaseTester", "onDataChange()");
                if (dataSnapshot.getChildrenCount() > 0) {
                    Log.i("FireBaseTester", "There are "+dataSnapshot.getChildrenCount()+" Users");
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                        String email = null;

                        if(userSnapshot.child("email") != null) {
                            //this is how you get the value of Email
                            email = userSnapshot.child("email").getValue().toString();
                            Log.i("FireBaseTester", "User Email:"+email+" PLAIN");
                        }
                        if(userSnapshot.child("waypoints") !=null){
                            if(userSnapshot.child("waypoints").getChildrenCount() > 0){
                                Firebase wayPointRef = userSnapshot.child("waypoints").getRef();

                                if(wayPointRef != null){
                                    wayPointRef.addValueEventListener(new ValueEventListener() {
                                        //this keeps per-user list of points
                                        List<MyWaypoint> userWayPointsList = new ArrayList<MyWaypoint>();
                                        @Override
                                        public void onDataChange(DataSnapshot wayPointsDataSnapshot) {
                                            if(wayPointsDataSnapshot.getChildrenCount() > 0){
                                                for (DataSnapshot wayPointsSnapshotChild : wayPointsDataSnapshot.getChildren()){
                                                    Log.i("FireBaseTester", "For-Loop :: wayPointsSnapshotChild.getValue() : "+wayPointsSnapshotChild.getValue());
                                                   if(wayPointsSnapshotChild.getChildrenCount()>0){

                                                       for (DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()){
                                                           //this is where we get the Lat and Lon
                                                           double latitude = Double.parseDouble(wayPointsChild.child("latitude").getValue().toString());
                                                           double longitude = Double.parseDouble(wayPointsChild.child("longitude").getValue().toString());
                                                           userWayPointsList.add(new MyWaypoint( latitude,  longitude));
                                                           Log.i("FireBaseTester","latitude = "+latitude+" , longitude = "+longitude);
                                                       }
                                                    }

                                                }
                                            }
                                            //here you can assign the points to the user
                                            Log.i("FireBaseTester","There are "+userWayPointsList.size()+ " Points for User");
                                        }

                                        @Override
                                        public void onCancelled(FirebaseError firebaseError) {
                                            Log.e("FireBaseTester", "onCancelled - wayPointRef Error is " + firebaseError.getMessage());
                                        }
                                    });
                                }
                            }
                            else{
                                Log.i("FireBaseTester", "No WayPoints Data Received");
                            }
                        }
                    }
                }
                else{
                    //no data?
                    Log.i("FireBaseTester", "No Data Received");
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Log.e("FireBaseTester", "onCancelled - Error is "+firebaseError.getMessage());
            }
        });

最新更新:您表示您更喜欢检索特定于用户的数据,而不是所有用户的列表(就像我在上面的代码中建议的那样)。下面是用户特定数据的代码。您的URL现在应该类似于:最终字符串userUrl=constants.firebase_url+“/users/”+muserid;

userRef.myFirebaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {                
                if (dataSnapshot.getChildrenCount() > 0) {
                    Log.i("FireBaseTester", "There are "+dataSnapshot.getChildrenCount()+" User Attributes");
                        String email = null;
                        if(dataSnapshot.child("email") != null) {
                            //this is how you get the value of Email
                            email = dataSnapshot.child("email").getValue().toString();
                            Log.i("FireBaseTester", "User Email:"+email+" PLAIN");
                        }
                        if(dataSnapshot.child("waypoints") !=null){
                            if(dataSnapshot.child("waypoints").getChildrenCount() > 0){
                                Firebase wayPointRef = dataSnapshot.child("waypoints").getRef();

                                if(wayPointRef != null){
                                    wayPointRef.addValueEventListener(new ValueEventListener() {
                                        //this keeps per-user list of points
                                        List<MyWaypoint> userWayPointsList = new ArrayList<MyWaypoint>();
                                        @Override
                                        public void onDataChange(DataSnapshot wayPointsDataSnapshot) {
                                            if(wayPointsDataSnapshot.getChildrenCount() > 0){
                                                for (DataSnapshot wayPointsSnapshotChild : wayPointsDataSnapshot.getChildren()){
                                                    Log.i("FireBaseTester", "For-Loop :: wayPointsSnapshotChild.getValue() : "+wayPointsSnapshotChild.getValue());
                                                    if(wayPointsSnapshotChild.getChildrenCount()>0){

                                                        for (DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()){
                                                            //this is where we get the Lat and Lon
                                                            double latitude = Double.parseDouble(wayPointsChild.child("latitude").getValue().toString());
                                                            double longitude = Double.parseDouble(wayPointsChild.child("longitude").getValue().toString());
                                                            userWayPointsList.add(new MyWaypoint( latitude,  longitude));
                                                            Log.i("FireBaseTester","latitude = "+latitude+" , longitude = "+longitude);
                                                        }
                                                    }

                                                }
                                            }
                                            //here you can assign the points to the user
                                            Log.i("FireBaseTester","There are "+userWayPointsList.size()+ " Points for User");
                                        }

                                        @Override
                                        public void onCancelled(FirebaseError firebaseError) {
                                            Log.e("FireBaseTester", "onCancelled - wayPointRef Error is " + firebaseError.getMessage());
                                        }
                                    });
                                }
                            }
                            else{
                                Log.i("FireBaseTester", "No WayPoints Data Received");
                            }
                        }
                }
                else{
                    //no data?
                    Log.i("FireBaseTester", "No User Attributes");
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Log.e("FireBaseTester", "onCancelled - Error is "+firebaseError.getMessage());
            }
        });

我希望这能让你轻松些。顺便说一下,向您展示使用RecycerView的代码将非常有用--在这种情况下,您可能希望在这里查看一个很好的示例

 类似资料:
  • 问题内容: 我试图从Android手机上按本机后退按钮时取消CountDownTimer。因此,我想覆盖onBackPressed方法,以取消计时器并返回到另一个活动,但只能执行一次。(返回主活动,如主页按钮)。 这是代码的一部分: 问题答案: onBackPressed应该在主活动类中 还尝试在应用的清单文件中指定父项和子项活动,例如 并尝试使用类似的东西并重建您的应用程序 如果仍然无法正常运行

  • 问题内容: 我试图用例如值替换指定的模式 这将返回找到模式的行。例如,这是得到的测试返回值之一 这是$$ test $$ 我遇到的问题是当我执行以下操作时 没发生什么事。我想念什么?有关更多信息, 所以我想做的就是每当遇到“ $$ test $$”时,它将用“替换”替换它。我没有发现“ $$ test $$”的问题,但是由于某种原因没有取代它。 问题答案: 您 正在 将back的结果分配给变量,对

  • 我想将我的React应用程序按块拆分,但遇到了一个问题。不呈现通过加载的子。下面的示例代码。 文件一: 文件二 然后检索下一个错误: 未捕获的错误:缩小的React错误#130;访问http://facebook.github.io/react/docs/error-decoder.html?invariant=130&args[]=未定义&args[]=获取完整消息,或使用非缩小的开发环境获取完

  • 问题内容: 我在这里有点困惑。如果我将变量传递给json_decode,它将不起作用: 第一个回显正确显示了我传递的JSON字符串,例如 第二个回显显示NULL。因此,我从第一个回显中获取了字符串,并编写了以下代码: 你怎么说,它向我展示了正确解码的数组。字符串绝对相同,我什至保留转义字符。也许是问题所在? 问题答案: 看起来您的服务器已启用。无论是将其禁用或运行通过使用它之前。

  • 问题内容: 我已经看过xpath教程并检查了许多其他帖子,因此我不确定自己缺少什么。我只是想通过xpath查找以下元素: 我已经尝试了很多事情,例如: 错误是。 我在MacBook上使用Firefox 任何建议将不胜感激。 问题答案: (*)-任何标记名

  • 在我活动页面之一的片段中,我希望从用户处获得权限,并在授予权限后执行一些操作,但不起作用,且在授予权限后看不到任何日志