gpt4 book ai didi

java - FirebaseRecyclerAdapter 不适合我

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:57 26 4
gpt4 key购买 nike

我需要父键及其子键来表示 RecyclerView 中的纬度和经度列表。我想我需要对 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;
}
}

我的 Activity

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);
}
}

FATAL EXCEPTION: main Process: com.example.rasmusjosefsson.rjcar, PID: 19134 com.firebase.client.FirebaseException: Failed to bounce to type at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185) at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161) at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150) at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1170) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.H

最佳答案

更新答案将您的 Firebase 网址更改为 final String userUrl = Constants.FIREBASE_URL + "/user"(删除网址的其余部分)。我在这段新代码中所做的是迭代数据并为每个经纬度对创建 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 现在应类似于:final String 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());
}
});

我希望这能让您的事情变得更轻松。顺便说一句,显示使用 RecyclerView 的代码对您来说非常有用 - 在这种情况下您可能想看一个很好的示例 here .

关于java - FirebaseRecyclerAdapter 不适合我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37116486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com