我制作了一个测试应用,试图找出Firestore数据库和RecyclerAdapter
的功能。我按照了一些关于这个主题的教程,但我得到的最接近的是这个,当应用程序加载时,它可以看到数据库对象,因为它加载了正确的数字,当我这样做时有3个存储。但是当我设置句柄文本时,它会给我一个空字符串。
public class MainActivity extends AppCompatActivity {
FirebaseFirestore mDatabase;
private FirestoreRecyclerAdapter mFirestoreRecyclerAdapter;
RecyclerView mRecyclerView;
LinearLayoutManager mLayoutManager;
Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initRecyclerAdapter();
setSupportActionBar(mToolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public void onStart() {
super.onStart();
mFirestoreRecyclerAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mFirestoreRecyclerAdapter.stopListening();
}
public void init() {
mToolbar = findViewById(R.id.toolbar);
mRecyclerView = findViewById(R.id.recycler_view);
mLayoutManager = new LinearLayoutManager(getApplicationContext(),
LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mDatabase = FirebaseFirestore.getInstance();
}
private void initRecyclerAdapter() {
Query query = mDatabase.collection("users");
FirestoreRecyclerOptions<User> response = new
FirestoreRecyclerOptions.Builder<User>()
.setQuery(query, User.class)
.build();
mFirestoreRecyclerAdapter = new FirestoreRecyclerAdapter<User,
MainActivity.UserHolder>(response) {
@Override
public MainActivity.UserHolder onCreateViewHolder(ViewGroup
parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new UserHolder(view); }
@Override
protected void onBindViewHolder(MainActivity.UserHolder holder,
int position, User model) {
holder.setFirstName(model.getFirstName());
holder.setLastName(model.getLastName());
holder.setBornDate(model.getBornDate());
}
};
mFirestoreRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mFirestoreRecyclerAdapter);
}
public class UserHolder extends RecyclerView.ViewHolder {
TextView textName;
CircleImageView imageView;
TextView textTitle;
TextView textCompany;
public View itemView;
public UserHolder(View itemView) {
super(itemView);
textName = itemView.findViewById(R.id.name);
textTitle = itemView.findViewById(R.id.title);
textCompany = itemView.findViewById(R.id.company);
this.itemView = itemView;
}
public void setFirstName(String name) {
textName.setText(name);
}
public void setLastName(String lastName) {
textTitle.setText(lastName);
}
public void setBornDate(String bornDate) {
textCompany.setText(bornDate);
}
}
}
public class User {
private String born;
private String first;
private String last;
public User() { }
public User(String born, String first, String last) {
this.born = born;
this.first = first;
this.last = last;
}
public String getFirstName() {
return first;
}
public void setFirstName(String first) {
this.first = first;
}
public String getLastName() {
return last;
}
public void setLastName(String last) {
this.last = last;
}
public String getBornDate() {
return born;
}
public void setBornDate(String born) {
this.born = born;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support:cardview-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation 'de.hdodenhof:circleimageview:2.0.0'
implementation 'com.google.firebase:firebase-core:11.4.2'
implementation 'com.google.firebase:firebase-firestore:11.4.2'
implementation 'com.firebaseui:firebase-ui-firestore:3.0.0'
implementation 'com.android.support.constraint:constraint-
layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
截图
答案 0 :(得分:0)
代码中的问题是您的所有字段都有错误的getter。因此,像born
这样的字段应该具有getBorn()
方法作为getter而不是getBornDate()
。对于first
和last
,正确的获取者为getFirst()
和getLast()
,而不是getFirstName()
和getLastName()
,因为它现在位于您的代码中。请参阅下面的模型类应该如何:
public class User {
private String born;
private String first;
private String last;
public User() { }
public User(String born, String first, String last) {
this.born = born;
this.first = first;
this.last = last;
}
public String getBorn() { return born; }
public void setBorn(String born) { this.born = born; }
public String getFirst() { return first; }
public void setFirst(String first) { this.first = first; }
public String getLast() { return last; }
public void setLast(String last) { this.last = last; }
}
其余代码是正确的。