我试图在CardView内使用TextView简单显示Firestore集合中的一些文本,但无济于事。目前,使用下面的代码,CardView成功显示,但其中不显示任何文本。我的TextView在RecyclerView内部。我正在按照youtube教程进行此项目,但是视频中的人获得了所需的输出,但我无法获得它。 https://youtu.be/lAGI6jGS4vs我不知道我要去哪里错了。
以下是代码, note_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:backgroundTint="@color/ic_launcher_background"
android:layout_marginEnd="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:id="@+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:ellipsize="end"
android:maxLines="1"
android:text="Name"
android:textColor="#FFEB3B" />
<TextView
android:id="@+id/text_view_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_name"
android:textColor="#FFEB3B"
android:layout_alignParentStart="true"
android:text="Phone" />
<TextView
android:id="@+id/text_view_address"
android:textColor="#FFEB3B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_phone"
android:layout_alignParentStart="true"
android:text="Address" />
<TextView
android:id="@+id/text_view_details"
android:textColor="#FFEB3B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_address"
android:layout_alignParentStart="true"
android:text="Details" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
Note.java
package com.example.authenticatorapp;
public class Note {
private String name;
private String phone;
private String address;
private String details;
public Note() {
}
public Note(String name,String phone,String address,String details){
this.name = name;
this.phone = phone;
this.address = address;
this.details = details;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getaddress() {
return address;
}
public void setaddress(String address) {
this.address = address;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
}
NoteAdapter.java
package com.example.authenticatorapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
public class NoteAdapter extends FirestoreRecyclerAdapter<Note,NoteAdapter.NoteHolder> {
public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull NoteHolder holder,int position,@NonNull Note model) {
holder.textViewName.setText(model.getName());
holder.textViewPhone.setText(model.getPhone());
holder.textViewAddress.setText(model.getaddress());
holder.textViewDetails.setText(model.getDetails());
}
@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
View v = LayoutInflater.from(parent.getcontext()).inflate(R.layout.note_item,parent,false);
return new NoteHolder(v);
}
class NoteHolder extends RecyclerView.ViewHolder {
TextView textViewName;
TextView textViewPhone;
TextView textViewAddress;
TextView textViewDetails;
public NoteHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
textViewPhone = itemView.findViewById(R.id.text_view_phone);
textViewAddress = itemView.findViewById(R.id.text_view_address);
textViewDetails = itemView.findViewById(R.id.text_view_details);
}
}
}
Receive.java(就像mainactivity.java一样)
package com.example.authenticatorapp;
import androidx.appcompat.app.AppCompatactivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
public class Receive extends AppCompatactivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef = db.collection("DonationDetails");
private NoteAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
setUpRecyclerView();
}
private void setUpRecyclerView() {
FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
.setQuery(notebookRef,Note.class)
.build();
adapter = new NoteAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setadapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
activity_receive.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:background="@drawable/images"
tools:context=".Receive">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
这是可能有助于@AlexMamo的数据库结构