我正在使用Cloud Firestore。我的Firestore数据库文档名称是用户,我想在Android RecyclerView和我的活动名称MainActivity中显示所有用户,并且我使用此XML文件activity_main.xml。
在跑步时会显示空白活动。
activity_main.xml中
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context="com.example.anuragtiwari.recyclerdemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/recyclerview_list"
android:layout_marginBottom="8dp"
android:layout_marginEnd="7dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="7dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.073"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
android:layout_alignParentTop="true">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView mUsersList;
private CollectionReference muser;
private FirestoreRecyclerAdapter adapter;
LinearLayoutManager linearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsersList = (RecyclerView) findViewById(R.id.recyclerview_list);
mUsersList.setLayoutManager(new LinearLayoutManager(this));
muser = FirebaseFirestore.getInstance().collection("users");
Query query = muser;
FirestoreRecyclerOptions<Users> options = new FirestoreRecyclerOptions.Builder<Users>()
.setQuery(query, Users.class).build();
FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<Users, FriendsHolder>(options)
{
@Override
public FriendsHolder onCreateViewHolder(ViewGroup group, int i)
{
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.single_user, group, false);
return new FriendsHolder(view);
}
@Override
public void onError(FirebaseFirestoreException e)
{
Log.e("error", e.getMessage());
}
@Override
protected void onBindViewHolder(FriendsHolder holder, int position, Users model) {
holder.nameText.setText(model.getName());
holder.emailText.setText(model.getEmail());
}
};
mUsersList.setAdapter(adapter);
}
private class FriendsHolder extends RecyclerView.ViewHolder
{
View mView;
public TextView nameText;
public TextView emailText;
public FriendsHolder(View itemview) {
super(itemview);
mView = itemview;
nameText = (TextView) mView.findViewById(R.id.single_user_name);
emailText = (TextView) mView.findViewById(R.id.single_user_email);
}
}
}
single_user.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_margin="15dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/single_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
android:text="Display Name"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/single_user_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/single_user_name"
android:layout_alignStart="@+id/single_user_name"
android:layout_below="@+id/single_user_name"
android:layout_marginTop="15dp"
android:text="User Email" />
<view
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="60dp"
/>
</RelativeLayout>
Users.java
public class Users
{
public String name;
public String email;
public Users(){
}
public String getName() {
return name;
}
public void setName(String name) {
name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
email = email;
}
public Users(String name, String email) {
this.name = name;
this.email = email;
}
}
**App Images**