【问题标题】:Android Firebase RecyclerAdapterAndroid Firebase RecyclerAdapter
【发布时间】:2017-05-11 18:54:59
【问题描述】:

我正在尝试使用 Firebase RecyclerAdapter 将 Firebase 数据库中的数据显示到 recyclerview 中。我对此很陌生,环顾四周。但我无法弄清楚这里出了什么问题。 PopulateViewHolder 中的 println,我在其中添加了它以查看该功能是否正常工作并且它不打印任何内容。 DeviceModel 中的那个也没有。这是我发生这一切的类的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class RecievedKeys extends AppCompatActivity {

    RecyclerView mRecyclerView;
    User user = new User();
    DatabaseReference mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Shared_with").child(user.getUid());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recieved_keys);
        mRecyclerView = (RecyclerView) findViewById(R.id.relLayout);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        final FirebaseRecyclerAdapter<DeviceModel, DeviceViewHolder> firebaseRecAdapter = new
                FirebaseRecyclerAdapter<DeviceModel, DeviceViewHolder>(DeviceModel.class, R.layout.device,
                        DeviceViewHolder.class, mDatabaseRef) {

                    @Override
                    protected void populateViewHolder(DeviceViewHolder viewHolder, DeviceModel model, int position) {
                        System.out.println("ViewHolder");
                        viewHolder.setName(model.getDevice_name());
                        viewHolder.setDevice_MAC(model.getDevice_MAC());
                    }
                };
        mRecyclerView.setAdapter(firebaseRecAdapter);
    }



    private class DeviceViewHolder extends RecyclerView.ViewHolder {
        View mView;
        public DeviceViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }
            public void setName(String mName){
            TextView name = (TextView)mView.findViewById(R.id.name);
            name.setText(mName);
            }

        public void setDevice_MAC(String macAdd){
            TextView mac = (TextView)mView.findViewById(R.id.MAC);
            mac.setText(macAdd);
        }

        }
    }
`

这是我的布局文件:

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootRel"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/relLayout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

这是我的 recycleradapter 模型:

public class DeviceModel {
    private String Device_MAC;
    private String Password;
    private String Device_name;

    public DeviceModel() {
    }

    public String getDevice_MAC() {
        System.out.println(Device_MAC+"From getter");
        return Device_MAC;
    }

    public void setDevice_MAC(String device_MAC) {
        Device_MAC = device_MAC;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }

    public String getDevice_name() {
        return Device_name;
    }

    public void setDevice_name(String device_name) {
        Device_name = device_name;
    }
}

这是我要显示的节点的数据库结构: enter image description here

我得到的只是一个空白屏幕。没有错误,甚至没有任何东西可以将我指向某个地方。任何帮助表示赞赏。

【问题讨论】:

  • 您正在使用user.getUid() 定义mDatabaseRefuser 在上一行定义:user = new User()。您确信这会产生有效的数据库引用吗?发布 User() 的构造函数。
  • 是的。我也尝试在 onCreate 方法中定义它。没用

标签: android firebase firebase-realtime-database android-recyclerview


【解决方案1】:

有错别字。你使用child("Shared_with"),数据库使用Shared_With(大写W)。

另外,您的数据库不包含DeviceModel 的实例。数据库中字段的名称必须与模型中的名称相对应。查看FirebaseUI documentation 中的描述。对于您在模型中定义的 getter/setter 名称,数据库实例应包含:device_MAC、password、device_Name。

【讨论】:

  • 谢谢!修复了这个问题,它给出了很多错误并且应用程序崩溃了。现在至少有一些东西
  • FirebaseUI docs 中的示例。
  • 好的,错误消失了,应用程序不再崩溃。回到它在做什么。什么都没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-10
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多