FirebaseRecyclerAdapter 对我不起作用
FirebaseRecyclerAdapter doesn't work for me
我需要父键及其子键来表示 RecyclerView 中的纬度和经度列表。我想我需要对 LatLngsModel item
中的FirebaseRecyclerAdapter
里面的 populateViewHolder
但无论我做什么,我都做错了。
我目前循环遍历所有父键及其子键,并将其添加到列表中...
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 URL 更改为 final String userUrl = Constants.FIREBASE_URL + "/users"
(去掉其余的 URL)。
我在这个新代码中所做的是遍历数据并为每个经纬度对创建 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.
我发现有效的解决方案是使用 List<List<String>>
从 onDataChange()
中检索 waypoints 并将它们放入嵌套列表中。
此嵌套列表稍后在 recyclerView 中使用,其中嵌套列表中的每个列表代表我的 recyclerView
中的一个 "View item"
private Firebase mRef;
private Firebase mUserRef;
private String mUserId;
List<List<String>> latitudeNlongitude = new ArrayList<>();
// FirebaseRecyclerAdapter<MyLatLng123, LatLngViewHolderBack2> mAdapter;
FirebaseRecyclerAdapter<MyWaypoint, 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/waypoints
final String userUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/waypoints";
mUserRef = new Firebase(userUrl);
if (mUserRef != null) {
mUserRef.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) {
// Temporary list
List<String> latLngListTemp = new ArrayList<>();
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());
Log.i("FireBaseTester", "latitude = " + latitude + " , longitude = " + longitude);
latLngListTemp.add(latitude + ", " + longitude);
}
// List containing a nested List<List<String>>
latitudeNlongitude.add(latLngListTemp);
}
}
}
//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");
}
mAdapter = new FirebaseRecyclerAdapter<MyWaypoint, LatLngViewHolderBack2>(MyWaypoint.class, R.layout.list_item, LatLngViewHolderBack2.class, mUserRef) {
@Override
protected void populateViewHolder(final LatLngViewHolderBack2 latLngViewHolder, MyWaypoint item, final int i) {
// Retrieves a List from the position (i) of the nested list at onDataChange --- > List<List<String>> latitudeNlongitude
List<String> latlngListIterator = latitudeNlongitude.get(i);
// New Empty list for each iteration
List<String> latLngListTemporary = new ArrayList<>();
// Adding items to a new empty list for use in next method with googles api.
for (String value : latlngListIterator) {
latLngListTemporary.add(value);
}
// Getting the start location and the endlocation
String startLatLng = latLngListTemporary.get(0);
String endLatLng = latLngListTemporary.get(latLngListTemporary .size() - 1);
// Retrieves the information from googles API in JSON and turns them into POJOS
我需要父键及其子键来表示 RecyclerView 中的纬度和经度列表。我想我需要对 LatLngsModel item
中的FirebaseRecyclerAdapter
里面的 populateViewHolder
但无论我做什么,我都做错了。
我目前循环遍历所有父键及其子键,并将其添加到列表中...
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 URL 更改为 final String userUrl = Constants.FIREBASE_URL + "/users"
(去掉其余的 URL)。
我在这个新代码中所做的是遍历数据并为每个经纬度对创建 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.
我发现有效的解决方案是使用 List<List<String>>
从 onDataChange()
中检索 waypoints 并将它们放入嵌套列表中。
此嵌套列表稍后在 recyclerView 中使用,其中嵌套列表中的每个列表代表我的 recyclerView
中的一个 "View item" private Firebase mRef;
private Firebase mUserRef;
private String mUserId;
List<List<String>> latitudeNlongitude = new ArrayList<>();
// FirebaseRecyclerAdapter<MyLatLng123, LatLngViewHolderBack2> mAdapter;
FirebaseRecyclerAdapter<MyWaypoint, 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/waypoints
final String userUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/waypoints";
mUserRef = new Firebase(userUrl);
if (mUserRef != null) {
mUserRef.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) {
// Temporary list
List<String> latLngListTemp = new ArrayList<>();
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());
Log.i("FireBaseTester", "latitude = " + latitude + " , longitude = " + longitude);
latLngListTemp.add(latitude + ", " + longitude);
}
// List containing a nested List<List<String>>
latitudeNlongitude.add(latLngListTemp);
}
}
}
//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");
}
mAdapter = new FirebaseRecyclerAdapter<MyWaypoint, LatLngViewHolderBack2>(MyWaypoint.class, R.layout.list_item, LatLngViewHolderBack2.class, mUserRef) {
@Override
protected void populateViewHolder(final LatLngViewHolderBack2 latLngViewHolder, MyWaypoint item, final int i) {
// Retrieves a List from the position (i) of the nested list at onDataChange --- > List<List<String>> latitudeNlongitude
List<String> latlngListIterator = latitudeNlongitude.get(i);
// New Empty list for each iteration
List<String> latLngListTemporary = new ArrayList<>();
// Adding items to a new empty list for use in next method with googles api.
for (String value : latlngListIterator) {
latLngListTemporary.add(value);
}
// Getting the start location and the endlocation
String startLatLng = latLngListTemporary.get(0);
String endLatLng = latLngListTemporary.get(latLngListTemporary .size() - 1);
// Retrieves the information from googles API in JSON and turns them into POJOS