我试图在一个带有cardview和firebase的片段中使用recyclerview(数据库中有所有的图像、名称和数量要传递到卡中),但我遇到了一个问题:
我的碎片里什么都没有!我的recyclerview不工作,我的卡不显示,我的碎片是空的,我的日志中没有错误。
以下是片段的java:
package com.example.progettocontab.assistenza;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.progettocontab.R;
import com.example.progettocontab.app.Articolo;
import com.example.progettocontab.app.ArticoloAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.FirebaseDatabase;
public class FragmentAssistenza extends Fragment {
RecyclerView recyclerView;
ArticoloAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_assistenza, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recview);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
FirebaseRecyclerOptions<Articolo> options =
new FirebaseRecyclerOptions.Builder<Articolo>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Articoli"), Articolo.class)
.build();
adapter = new ArticoloAdapter(options);
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
以下是片段的xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".assistenza.FragmentAssistenza">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
下面是适配器(带有viewholder类):
package com.example.progettocontab.app;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.progettocontab.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
public class ArticoloAdapter extends FirebaseRecyclerAdapter<Articolo, ArticoloAdapter.ViewHolder> {
public ArticoloAdapter(@NonNull FirebaseRecyclerOptions<Articolo> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, final int position, @NonNull Articolo model) {
holder.nome.setText(model.getNome());
holder.quantita.setText(model.getQuantita());
Glide.with(holder.img.getContext()).load(model.getImg()).into(holder.img);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), model.getNome(), Toast.LENGTH_SHORT).show();
}
});
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_article, parent, false);
return new ViewHolder(view);
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView img;
TextView nome;
TextView quantita;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.img);
nome = (TextView) itemView.findViewById(R.id.nameText);
quantita = (TextView) itemView.findViewById(R.id.amountText);
}
}
}
以下是阿替科洛的 java 语:
package com.example.progettocontab.app;
public class Articolo {
private String nome;
private String descrizione;
private String quantita;
private String img;
private int ID;
public Articolo(String nome, String descrizione, String quantita, String img, int ID) {
this.nome = nome;
this.descrizione = descrizione;
this.quantita = quantita;
this.img = img;
this.ID = ID;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getDescrizione() {
return descrizione;
}
public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}
public String getQuantita() {
return quantita;
}
public void setQuantita(String quantita) {
this.quantita = quantita;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
}
以下是cardview的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:cardCornerRadius="5dp"
android:elevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher"
android:layout_centerVertical="true"
android:id="@+id/img"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nameText"
android:text="This is Demo Name"
android:textStyle="bold"
android:textSize="25sp"
android:textColor="#000"
android:layout_toRightOf="@id/img"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/amountText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nameText"
android:layout_marginLeft="6dp"
android:layout_marginTop="12dp"
android:layout_toRightOf="@id/img"
android:text="This is Course Name"
android:textColor="#000"
android:textSize="25sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
3条答案
按热度按时间vx6bjr1n1#
试着移动
recyclerView.setAdapter(adapter)
在你之后startListening
所以,把它从onViewCreated()
,并将其添加到onStart()
```@Override
public void onStart() {
super.onStart();
adapter.startListening();
recyclerView.setAdapter(adapter);
}
pes8fvy92#
尝试将recyclerview转换为onviewcreated,因为此时,fragment的视图会被填充,您可以访问recyclerview并将其膨胀。
suzh9iv83#
我想你可能是因为
implementation 'com.google.firebase:firebase-messaging'
但是你还没有在你的清单中注册服务。因此,要么从gradle中删除该行(如果您不需要firebase的消息传递功能),要么将服务添加到清单中,如下所示: