- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要父键及其子键来表示 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/
谁能告诉我那一行的错误是什么。 这是显示的错误消息。 package com.example.souravkumar.sqaurewallpapers; import android.os.Bundl
谁能告诉我那一行的错误是什么。 这是显示的错误消息。 package com.example.souravkumar.sqaurewallpapers; import android.os.Bundl
这个问题已经有答案了: firebaserecycleradapter() cannot be applied to FirebaseRecyclerAdapter (5 个回答) 已关闭 4 年前。
这个问题已经有答案了: firebaserecycleradapter() cannot be applied to FirebaseRecyclerAdapter (5 个回答) 已关闭 3 年前。
谁能告诉我错误是什么 enter image description here 这是我的代码 package com.andisofttechnology.myapplication.Model; i
即使在 gradle 依赖项中添加 firebase ui 后,我在尝试使用它时也无法解析 FirebaseRecyclerAdapter。提前致谢 最佳答案 我遇到了同样的问题,通过在 build.
我是 Android 编程新手。我已经能够在 firebase 中创建用户配置文件表,并且可以在 recyclerview 中显示用户配置文件表中的用户数据,但我无法让名为“Arbrg”的产品表在 f
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 4 年前。 我的应用程序在登录后立即崩溃,并
FirebaseRecyclerAdapter - populateViewHolder 第一次运行时没有填充数据,但是当我关闭应用程序并打开它时,数据绑定(bind)在 RecyclerView 中
我为单个项目创建了一个模型类和布局,然后创建了 View 持有者和 firebaseRecyclerAdapter,如下面的代码所示,当我运行它时出现了这个错误 com.google.
我有一个连接到 Firebase 的应用程序。我想用来自 firebase 的数据填充 recyclerview。在此之前,我制作了一个应用程序并且运行良好。现在我使用相同的方法,当我说 adapte
我正在使用 Firebase 创建一个聊天应用。我正在使用来自 firebase-ui 的 recycleView 和 FirebaseRecyclerAdapter 来显示所有消息。我的问题是当有人
@Override public void onStart() { super.onStart(); FirebaseRecyclerAdapter firebaseRecyclerA
我正在使用 FirebaseRecyclerAdapter(FirebaseUI 的一部分)将 Firebase 数据加载到 RecyclerView 中,但是在文档或在线的任何地方我都找不到检测它是
我需要父键及其子键来表示 RecyclerView 中的纬度和经度列表。我想我需要对 FirebaseRecyclerAdapter 内的 populateViewHolder 中的 LatLngsM
这个问题已经有答案了: FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1 (2 个回答)
代码: FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder()
目前还不清楚如何使用自定义查询填充 FirebaseRecyclerAdapter。 定义一个引用。此处:DatabaseReference ref = mDatabase.getReference(
我的标题有点难以理解,但基本上当我将项目添加到我的数据库时,它应该显示在 RecyclerView 中。现在在我的 RecyclerView 中我有两个布局,但问题是我的数据库的第一项在我的其他布局中
浏览 Firebase Android 友好聊天代码实验室 - https://codelabs.developers.google.com/codelabs/firebase-android/ -
我是一名优秀的程序员,十分优秀!