无法使用FirebaseRecyclerAdapter加载多行

时间:2016-08-09 10:39:27

标签: android firebase firebase-realtime-database firebaseui

我正在尝试使用firebaseRecyclerAdapter从firebase获取数据。但我无法将firebase数据库中的所有数据填充到RecyclerView。 数据库如下:

Firebase Database

我得到的输出仅来自“qqq”行

rvlist类:

public class rvlist {
String name, propellant;

public rvlist() {
}

public rvlist(String name, String propellant) {
    this.name = name;
    this.propellant = propellant;
}

public String getName() {
    return name;
}

public String getPropellant() {
    return propellant;
    }
} 

MyViewHolder:

public class MyViewHolder extends RecyclerView.ViewHolder {
View mView;

public MyViewHolder(View itemView) {
    super(itemView);
    mView = itemView;
}

public void setName(String name) {
    TextView field = (TextView) mView.findViewById(R.id.a);
    field.setText(name);
}

public void setPropellant(String text) {
    TextView field = (TextView) mView.findViewById(R.id.b);
    field.setText(text);
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity {

DatabaseReference mRef;
RecyclerView rv;
ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    pb = (ProgressBar)findViewById(R.id.pb);
    pb.setVisibility(View.VISIBLE);
    rv = (RecyclerView) findViewById(R.id.rvv);
    rv.setLayoutManager(new LinearLayoutManager(this));

    mRef = FirebaseDatabase.getInstance().getReference().child("table");
}

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<rvlist, MyViewHolder> recyclerAdapter = new FirebaseRecyclerAdapter<rvlist, MyViewHolder>
            (rvlist.class, R.layout.model, MyViewHolder.class, mRef) {
        @Override
        public void populateViewHolder(MyViewHolder itemsViewHolder, rvlist items, int position) {
            pb.setVisibility(View.GONE);
            itemsViewHolder.setName(items.getName());
            itemsViewHolder.setPropellant(items.getPropellant());
        }
    };
    rv.setAdapter(recyclerAdapter);
    }
}

1 个答案:

答案 0 :(得分:0)

确保您的RecyclerView和适配器layout_height设置为 wrap_content ,而不是match_parent

<android.support.v7.widget.RecyclerView
        android:id="@+id/recv_location"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="LinearLayoutManager"
        ...>
</android.support.v7.widget.RecyclerView>