代码之家  ›  专栏  ›  技术社区  ›  Chad Davis

Android-Java-Firestore在我的项目中没有在Recyclerview中显示数据

  •  -1
  • Chad Davis  · 技术社区  · 4 年前

    我试图从firestore获取数据,但我的代码似乎不起作用。如果代码中有错误,你能告诉我吗?我再也受不了了(

    --------------------------------------//ArtistModel

    public class ArtistsModel {
    
    public ArtistsModel() {}
    
    String artistName, artistClass;
    
    public  ArtistsModel(String artistName, String artistClass) {
        this.artistName = artistName;
        this.artistClass = artistClass;
    }
    
    public String getArtistName() {
        return artistName;
    }
    
    public void setArtistName(String artistName) {
        this.artistName = artistName;
    }
    
    public String getArtistClass() {
        return artistClass;
    }
    
    public void setArtistClass(String artistClass) {
        this.artistClass = artistClass;
    }
    }
    

    --------------------------------------------------------------------------//ArtistAdapter

           public class ArtistsAdapter extends 
         FirestoreRecyclerAdapter<ArtistsModel, ArtistsAdapter.holder> {
    
    
    /**
     * Create a new RecyclerView adapter that listens to a Firestore Query.  See {@link
     * FirestoreRecyclerOptions} for configuration options.
     *
     * @param options
     */
    public ArtistsAdapter(@NonNull FirestoreRecyclerOptions<ArtistsModel> options) {
        super(options);
    }
    
    @Override
    protected void onBindViewHolder(@NonNull holder holder, int position, @NonNull ArtistsModel model) {
    
        holder.artistName.setText(model.getArtistName());
        holder.artistClass.setText(model.getArtistClass());
    
    }
    
    @NonNull
    @Override
    public holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.artistrow, parent, false);
        return new holder(view);
    }
    
    class holder extends RecyclerView.ViewHolder{
    
        TextView artistName, artistClass;
    
    
        public holder(@NonNull View itemView) {
            super(itemView);
            View view = itemView;
    
            artistName = view.findViewById(R.id.tvartist_name);
            artistClass = view.findViewById(R.id.tvartist_class);
    
        }
    }
    
     }
    

    --------------------------------------------------------------------------//主要活动

    RecyclerView recView;
    FirebaseFirestore fStore;
    ArtistsAdapter artistsAdapter;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreatye(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        recView = findViewById(R.id.recyclerView);
        recView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    
        fStore = FirebaseFirestore.getInstance();
        Query query = fStore.collection("Artists");
    
        FirestoreRecyclerOptions<ArtistsModel> options =
                new FirestoreRecyclerOptions.Builder<ArtistsModel>()
                        .setQuery(query, ArtistsModel.class)
                        .build();
    
        artistsAdapter = new ArtistsAdapter(options);
        recView.setAdapter(artistsAdapter);
    
        artistsAdapter.startListening();
      
      }
      }
    

    --------------------------------------------------------------------------//艺术家行XML

     <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="8dp"
    app:cardBackgroundColor="@color/background_item">
    
    <androidx.cardview.widget.CardView
        android:id="@+id/cdartist_card"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        app:cardBackgroundColor="@color/background_item"
        app:cardCornerRadius="5dp">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/tvartist_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:fontFamily="@font/ppsemi"
                android:text="Jméno umělce"
                android:textColor="#FFFFFF"
                app:layout_constraintStart_toEndOf="@+id/ivartist_photo"
                app:layout_constraintTop_toTopOf="parent" />
    
            <ImageView
                android:id="@+id/ivartist_photo"
                android:layout_width="70dp"
                android:layout_height="70dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0"
                app:srcCompat="@drawable/profile_template" />
    
            <TextView
                android:id="@+id/tvartist_class"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/pplight"
                android:text="Zařazení umělce"
                android:textColor="#FFFFFF"
                android:textSize="12sp"
                android:translationY="-5dp"
                app:layout_constraintStart_toStartOf="@+id/tvartist_name"
                app:layout_constraintTop_toBottomOf="@+id/tvartist_name" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
    

    enter image description here

    0 回复  |  直到 4 年前