来自Firestore数据库的回收视图中的内容不会显示出来。

huangapple go评论71阅读模式
英文:

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(&quot;uID&quot;);

        Log.d(&quot;Appointment_History&quot;,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(&quot;uID&quot;,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(&quot;uID&quot;,uID);
                startActivity(to_Account);
            }
        });
    }

    private void setUpRecyclerView(String uID){
        FirebaseFirestore db = FirebaseFirestore.getInstance();

        Query query = db.collection(&quot;Appointment&quot;).whereEqualTo(&quot;user_id&quot;,uID);

        FirestoreRecyclerOptions&lt;Appointment&gt; options = new FirestoreRecyclerOptions.Builder&lt;Appointment&gt;()
                .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(&quot;Age&quot;)
    public String getAge() {
        return Age;
    }

    @PropertyName(&quot;Date&quot;)
    public String getDate() {
        return Date;
    }

    @PropertyName(&quot;Height&quot;)
    public String getHeight() {
        return Height;
    }

    @PropertyName(&quot;Queue&quot;)
    public String getQueue() {
        return Queue;
    }

    @PropertyName(&quot;Weight&quot;)
    public String getWeight() {
        return Weight;
    }

    @PropertyName(&quot;clinic_id&quot;)
    public String getClinic_id() {
        return clinic_id;
    }

    @PropertyName(&quot;clinic_treatment&quot;)
    public String getClinic_treatment() {
        return clinic_treatment;
    }

    @PropertyName(&quot;doctor_id&quot;)
    public String getDoctor_id() {
        return doctor_id;
    }

    @PropertyName(&quot;symptom_description&quot;)
    public String getSymptom_description() {
        return symptom_description;
    }

    @PropertyName(&quot;user_id&quot;)
    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&lt;Appointment,Adapter.ViewHolder&gt; {

    public Adapter(@NonNull FirestoreRecyclerOptions&lt;Appointment&gt; 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):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;ScrollView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.Appointment_History&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;ImageView
android:layout_width=&quot;203dp&quot;
android:layout_height=&quot;73dp&quot;
android:layout_alignParentLeft=&quot;true&quot;
android:layout_alignParentTop=&quot;true&quot;
android:layout_marginTop=&quot;20dp&quot;
android:src=&quot;@drawable/nudmhor_logo&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:layout_width=&quot;120dp&quot;
android:layout_height=&quot;25dp&quot;
android:layout_marginLeft=&quot;30dp&quot;
android:layout_marginTop=&quot;96dp&quot;
android:fontFamily=&quot;@font/prompt_regular&quot;
android:text=&quot;ประวัติการจอง&quot;
android:textColor=&quot;#607D85&quot;
android:textSize=&quot;16dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.047&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;com.google.android.material.imageview.ShapeableImageView
android:layout_width=&quot;320dp&quot;
android:layout_height=&quot;6dp&quot;
android:layout_marginLeft=&quot;10dp&quot;
android:layout_marginTop=&quot;120dp&quot;
android:background=&quot;@drawable/shape_line&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.429&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;com.google.android.material.imageview.ShapeableImageView
android:layout_width=&quot;390dp&quot;
android:layout_height=&quot;60dp&quot;
android:layout_marginLeft=&quot;-17dp&quot;
android:layout_marginTop=&quot;590dp&quot;
android:background=&quot;@drawable/bartab&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.046&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/button_main&quot;
android:layout_width=&quot;37dp&quot;
android:layout_height=&quot;30dp&quot;
android:layout_marginTop=&quot;595dp&quot;
android:layout_marginLeft=&quot;-1010dp&quot;
android:background=&quot;@drawable/main_icon_fade&quot;
android:fontFamily=&quot;@font/prompt_regular&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.804&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:layout_width=&quot;50dp&quot;
android:layout_height=&quot;15dp&quot;
android:layout_marginLeft=&quot;45dp&quot;
android:layout_marginTop=&quot;620dp&quot;
android:fontFamily=&quot;@font/prompt_regular&quot;
android:text=&quot;หน้าหลัก&quot;
android:textColor=&quot;#686868&quot;
android:textSize=&quot;12dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.047&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/button_appointment_history&quot;
android:layout_width=&quot;36dp&quot;
android:layout_height=&quot;30dp&quot;
android:layout_marginTop=&quot;595dp&quot;
android:layout_marginLeft=&quot;-480dp&quot;
android:background=&quot;@drawable/appointment_icon&quot;
android:fontFamily=&quot;@font/prompt_regular&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.804&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:layout_width=&quot;80dp&quot;
android:layout_height=&quot;15dp&quot;
android:layout_marginLeft=&quot;140dp&quot;
android:layout_marginTop=&quot;620dp&quot;
android:fontFamily=&quot;@font/prompt_regular&quot;
android:text=&quot;ประวัติการจอง&quot;
android:textColor=&quot;#96C9BA&quot;
android:textSize=&quot;12dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.047&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/button_account&quot;
android:layout_width=&quot;40dp&quot;
android:layout_height=&quot;28dp&quot;
android:layout_marginTop=&quot;598dp&quot;
android:layout_marginLeft=&quot;30dp&quot;
android:background=&quot;@drawable/account_icon_fade&quot;
android:fontFamily=&quot;@font/prompt_regular&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.804&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;15dp&quot;
android:layout_marginLeft=&quot;265dp&quot;
android:layout_marginTop=&quot;620dp&quot;
android:fontFamily=&quot;@font/prompt_regular&quot;
android:text=&quot;บัญชี&quot;
android:textColor=&quot;#686868&quot;
android:textSize=&quot;12dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.047&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id = &quot;@+id/appointment_recyclerview&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_marginTop=&quot;130dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.047&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/ScrollView&gt;

XML file(appointment_entry.xml):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.cardview.widget.CardView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
app:cardElevation=&quot;12dp&quot;
app:cardCornerRadius=&quot;12dp&quot;
android:layout_margin=&quot;16dp&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;16dp&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;horizontal&quot;
android:layout_gravity=&quot;center_horizontal&quot;&gt;
&lt;TextView
android:id=&quot;@+id/clinic_name&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:textStyle=&quot;bold&quot;/&gt;
&lt;/LinearLayout&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;horizontal&quot;
android:layout_gravity=&quot;center_horizontal&quot;&gt;
&lt;TextView
android:id=&quot;@+id/time&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
/&gt;
&lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;

**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.

huangapple
  • 本文由 发表于 2023年6月9日 00:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定