英文:
The content in recycler view retrieved from Firestore Database won't show up
问题
I tried to put the query content into the RecyclerView but it doesn't show up.
Here's the code:
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
// ... (other imports)
public class Appointment_History extends AppCompatActivity {
String uID;
Adapter adapter;
Button main, account;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appointment_history);
// ... (other code)
}
private void setUpRecyclerView(String uID) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
Query query = db.collection("Appointment").whereEqualTo("user_id", uID);
FirestoreRecyclerOptions<Appointment> options = new FirestoreRecyclerOptions.Builder<Appointment>()
.setQuery(query, Appointment.class)
.build();
adapter = new Adapter(options);
RecyclerView recyclerView = findViewById(R.id.appointment_recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
// ... (other methods and code)
}
model class:
import com.google.firebase.firestore.PropertyName;
public class Appointment {
// ... (class fields and constructor)
@PropertyName("Age")
public String getAge() {
return Age;
}
@PropertyName("Date")
public String getDate() {
return Date;
}
// ... (other getters)
}
Adapter:
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
// ... (other imports)
public class Adapter extends FirestoreRecyclerAdapter<Appointment, Adapter.ViewHolder> {
public Adapter(@NonNull FirestoreRecyclerOptions<Appointment> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Appointment model) {
holder.clinic_name.setText(model.getClinic_treatment());
holder.time.setText(model.getDate_Queue());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.appointment_entry, parent, false);
return new ViewHolder(v);
}
// ... (ViewHolder class and other methods)
}
XML File(appointment_history.xml):
<!-- Your XML layout code -->
XML file(appointment_entry.xml):
<!-- Your XML layout code -->
Data Structure: Link to Image
Please let me know if you encounter any issues or need further assistance. Thank you for reading.
英文:
I tried to put the query content into the RecyclerView but it doesn't show up.
Here's the code:
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class Appointment_History extends AppCompatActivity {
String uID;
Adapter adapter;
Button main,account;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appointment_history);
//The
Intent intent = getIntent();
uID = intent.getStringExtra("uID");
Log.d("Appointment_History",uID);
setUpRecyclerView(uID);
main = findViewById(R.id.button_main);
account = findViewById(R.id.button_account);
main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent to_Account = new Intent(Appointment_History.this,Main.class);
to_Account.putExtra("uID",uID);
startActivity(to_Account);
}
});
account.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent to_Account = new Intent(Appointment_History.this,Account.class);
to_Account.putExtra("uID",uID);
startActivity(to_Account);
}
});
}
private void setUpRecyclerView(String uID){
FirebaseFirestore db = FirebaseFirestore.getInstance();
Query query = db.collection("Appointment").whereEqualTo("user_id",uID);
FirestoreRecyclerOptions<Appointment> options = new FirestoreRecyclerOptions.Builder<Appointment>()
.setQuery(query, Appointment.class)
.build();
adapter = new Adapter(options);
RecyclerView recyclerView = findViewById(R.id.appointment_recyclerview);
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();
}
}
model class:
import com.google.firebase.firestore.PropertyName;
public class Appointment {
private String Age;
private String Date;
private String Height;
private String Queue;
private String Weight;
private String clinic_id;
private String clinic_treatment;
private String doctor_id;
private String symptom_description;
private String user_id;
public Appointment(){}
public Appointment(String age, String date, String height, String queue, String weight, String clinic_id, String clinic_treatment, String doctor_id, String symptom_description, String user_id){
this.Age = age;
this.Date = date;
this.Height = height;
this.Queue = queue;
this.Weight = weight;
this.clinic_id = clinic_id;
this.clinic_treatment = clinic_treatment;
this.doctor_id = doctor_id;
this.symptom_description = symptom_description;
this.user_id = user_id;
}
@PropertyName("Age")
public String getAge() {
return Age;
}
@PropertyName("Date")
public String getDate() {
return Date;
}
@PropertyName("Height")
public String getHeight() {
return Height;
}
@PropertyName("Queue")
public String getQueue() {
return Queue;
}
@PropertyName("Weight")
public String getWeight() {
return Weight;
}
@PropertyName("clinic_id")
public String getClinic_id() {
return clinic_id;
}
@PropertyName("clinic_treatment")
public String getClinic_treatment() {
return clinic_treatment;
}
@PropertyName("doctor_id")
public String getDoctor_id() {
return doctor_id;
}
@PropertyName("symptom_description")
public String getSymptom_description() {
return symptom_description;
}
@PropertyName("user_id")
public String getUser_id() {
return user_id;
}
public String getDate_Queue(){return getDate()+getQueue();}
}
Adapter:
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 Adapter extends FirestoreRecyclerAdapter<Appointment,Adapter.ViewHolder> {
public Adapter(@NonNull FirestoreRecyclerOptions<Appointment> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Appointment model) {
holder.clinic_name.setText(model.getClinic_treatment());
holder.time.setText(model.getDate_Queue());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.appointment_entry,parent,false);
return new ViewHolder(v);
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView clinic_name,time;
public ViewHolder(View view){
super(view);
clinic_name = view.findViewById(R.id.clinic_name);
time = view.findViewById(R.id.time);
}
}
}
XML File(appointment_history.xml):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".Appointment_History">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="203dp"
android:layout_height="73dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:src="@drawable/nudmhor_logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="120dp"
android:layout_height="25dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="96dp"
android:fontFamily="@font/prompt_regular"
android:text="ประวัติการจอง"
android:textColor="#607D85"
android:textSize="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.047"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="320dp"
android:layout_height="6dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="120dp"
android:background="@drawable/shape_line"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.429"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="390dp"
android:layout_height="60dp"
android:layout_marginLeft="-17dp"
android:layout_marginTop="590dp"
android:background="@drawable/bartab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_main"
android:layout_width="37dp"
android:layout_height="30dp"
android:layout_marginTop="595dp"
android:layout_marginLeft="-1010dp"
android:background="@drawable/main_icon_fade"
android:fontFamily="@font/prompt_regular"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.804"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="50dp"
android:layout_height="15dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="620dp"
android:fontFamily="@font/prompt_regular"
android:text="หน้าหลัก"
android:textColor="#686868"
android:textSize="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.047"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_appointment_history"
android:layout_width="36dp"
android:layout_height="30dp"
android:layout_marginTop="595dp"
android:layout_marginLeft="-480dp"
android:background="@drawable/appointment_icon"
android:fontFamily="@font/prompt_regular"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.804"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="80dp"
android:layout_height="15dp"
android:layout_marginLeft="140dp"
android:layout_marginTop="620dp"
android:fontFamily="@font/prompt_regular"
android:text="ประวัติการจอง"
android:textColor="#96C9BA"
android:textSize="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.047"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_account"
android:layout_width="40dp"
android:layout_height="28dp"
android:layout_marginTop="598dp"
android:layout_marginLeft="30dp"
android:background="@drawable/account_icon_fade"
android:fontFamily="@font/prompt_regular"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.804"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="30dp"
android:layout_height="15dp"
android:layout_marginLeft="265dp"
android:layout_marginTop="620dp"
android:fontFamily="@font/prompt_regular"
android:text="บัญชี"
android:textColor="#686868"
android:textSize="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.047"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id = "@+id/appointment_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="130dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.047"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
XML file(appointment_entry.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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="12dp"
app:cardCornerRadius="12dp"
android:layout_margin="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/clinic_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
**Data Structure:
https://i.stack.imgur.com/CGmzx.png
Please let me know, the cause of issue and solution. Thank you for reading.
答案1
得分: 0
The problem with your code lies in the fact that you're using:
recyclerView.setHasFixedSize(true);
To solve this simply remove the above line of code and your data will be displayed correctly. Remember that RecyclerView#setHasFixedSize():
RecyclerView can perform several optimizations if it can know in advance that RecyclerView's size is not affected by the adapter contents. RecyclerView can still change its size based on other factors (e.g. its parent's size) but this size calculation cannot depend on the size of its children or contents of its adapter (except the number of items in the adapter).
If your use of RecyclerView falls into this category, set this to true. It will allow RecyclerView to avoid invalidating the whole layout when its adapter contents change.
So in your case, that method call is not needed.
英文:
The problem with your code lies in the fact that you're using:
recyclerView.setHasFixedSize(true);
To solve this simply remove the above line of code and your data will be displayed correctly. Remember that RecyclerView#setHasFixedSize():
> RecyclerView can perform several optimizations if it can know in advance that RecyclerView's size is not affected by the adapter contents. RecyclerView can still change its size based on other factors (e.g. its parent's size) but this size calculation cannot depend on the size of its children or contents of its adapter (except the number of items in the adapter).
> If your use of RecyclerView falls into this category, set this to true. It will allow RecyclerView to avoid invalidating the whole layout when its adapter contents change.
So in your case, that method call is not needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论