英文:
RecyclerView is always blank in activity
问题
当我运行我的应用程序时,RecyclerView
在活动中总是显示为空白。我使用了普通的RecyclerView
和FirebaseAdapter
,但列表没有显示任何数据。数据在实时数据库和云 Firestore 上都可用,但我在RecyclerView
中没有获取到任何数据。
适配器类
public class FeedsAdapter extends FirestoreRecyclerAdapter<Fmodel, FeedsAdapter.myFeedViewHolder> {
public FeedsAdapter(@NonNull FirestoreRecyclerOptions<Fmodel> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull myFeedViewHolder holder, int position, @NonNull Fmodel model) {
holder.name.setText(model.getFname());
holder.mobile.setText(model.getFmobile());
holder.address.setText(model.getFaddress());
holder.message.setText(model.getFmessage());
}
@NonNull
@Override
public myFeedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feedbacks, parent, false);
return new myFeedViewHolder(view);
}
class myFeedViewHolder extends RecyclerView.ViewHolder {
TextView name, mobile, address, message;
public myFeedViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
mobile = itemView.findViewById(R.id.mobile);
address = itemView.findViewById(R.id.address);
message = itemView.findViewById(R.id.messege);
}
}
}
Activity.xml
public class FeedsActivity extends AppCompatActivity {
private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
private CollectionReference collectionReference = firebaseFirestore.collection("FeedBacks");
private FeedsAdapter feedsAdapter;
RecyclerView feedRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feeds);
setUpRecyclerView();
}
private void setUpRecyclerView() {
Query query = collectionReference.orderBy("Fname", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Fmodel> options = new FirestoreRecyclerOptions.Builder<Fmodel>().setQuery(query, Fmodel.class).build();
feedsAdapter = new FeedsAdapter(options);
feedRecyclerView = (RecyclerView) findViewById(R.id.feedrecycler);
feedRecyclerView.setHasFixedSize(true);
feedRecyclerView.setLayoutManager(new LinearLayoutManager(this));
feedRecyclerView.setAdapter(feedsAdapter);
}
@Override
protected void onStart() {
super.onStart();
feedsAdapter.startListening();
Toast.makeText(this, "fetch on", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
super.onStop();
feedsAdapter.stopListening();
Toast.makeText(this, "fetch off", Toast.LENGTH_SHORT).show();
}
}
模型类
package gk.com.Model;
public class Fmodel {
private String fname;
private String faddress;
private String fmobile;
private String fmessage;
public Fmodel() {
}
public Fmodel(String fname, String faddress, String fmobile, String fmessage) {
this.fname = fname;
this.faddress = faddress;
this.fmobile = fmobile;
this.fmessage = fmessage;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getFaddress() {
return faddress;
}
public void setFaddress(String faddress) {
this.faddress = faddress;
}
public String getFmobile() {
return fmobile;
}
public void setFmobile(String fmobile) {
this.fmobile = fmobile;
}
public String getFmessage() {
return fmessage;
}
public void setFmessage(String fmessage) {
this.fmessage = fmessage;
}
}
英文:
When I run my application, the RecyclerView
is always shown blank in the activity. I did code with the normal RecyclerView
as well firebaseRecyclerOptions helps with FirebaseAdapter
, but lists not showing any data. The data is available on both Realtime Database and Cloud FireStore, but I don't get any data in RecyclerView
.
Adapter class
public class FeedsAdapter extends FirestoreRecyclerAdapter <Fmodel,FeedsAdapter.myFeedViewHolder> {
public FeedsAdapter(@NonNull FirestoreRecyclerOptions <Fmodel> options) {
super ( options );
}
@Override
protected void onBindViewHolder(@NonNull myFeedViewHolder holder, int position, @NonNull Fmodel model) {
holder.name.setText ( model.getFname () );
holder.mobile.setText ( model.getFmobile () );
holder.address.setText ( model.getFaddress ());
holder.message.setText ( model.getFmessage ());
}
@NonNull
@Override
public myFeedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.feedbacks,parent,false );
return new myFeedViewHolder ( view );
}
class myFeedViewHolder extends RecyclerView.ViewHolder{
TextView name,mobile,address,message;
public myFeedViewHolder(@NonNull View itemView) {
super ( itemView );
name = itemView.findViewById ( R.id.name );
mobile = itemView.findViewById ( R.id.mobile );
address = itemView.findViewById ( R.id.address );
message = itemView.findViewById ( R.id.messege );
}
}
}
Activity.xml
public class FeedsActivity extends AppCompatActivity {
private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance ( );
private CollectionReference collectionReference = firebaseFirestore.collection ( "FeedBacks" );
private FeedsAdapter feedsAdapter;
RecyclerView feedRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_feeds );
setUpRecyclerView();
}
private void setUpRecyclerView() {
Query query = collectionReference.orderBy ( "Fname", Query.Direction.ASCENDING );
FirestoreRecyclerOptions <Fmodel> options = new FirestoreRecyclerOptions.Builder<Fmodel>().setQuery ( query,Fmodel.class ).build ();
feedsAdapter = new FeedsAdapter ( options );
feedRecyclerView = (RecyclerView) findViewById ( R.id.feedrecycler );
feedRecyclerView.setHasFixedSize ( true );
feedRecyclerView.setLayoutManager ( new LinearLayoutManager ( this ) );
feedRecyclerView.setAdapter ( feedsAdapter);
}
@Override
protected void onStart() {
super.onStart ( );
feedsAdapter.startListening ();
Toast.makeText ( this, "fetch on", Toast.LENGTH_SHORT ).show ( );
}
@Override
protected void onStop() {
super.onStop ( );
feedsAdapter.stopListening ();
Toast.makeText ( this, "fetch off", Toast.LENGTH_SHORT ).show ( );
}
}
model class
package gk.com.Model;
public class Fmodel {
private String fname;
private String faddress;
private String fmobile;
private String fmessage;
public Fmodel() {
}
public Fmodel(String fname, String faddress, String fmobile, String fmessage) {
this.fname = fname;
this.faddress = faddress;
this.fmobile = fmobile;
this.fmessage = fmessage;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getFaddress() {
return faddress;
}
public void setFaddress(String faddress) {
this.faddress = faddress;
}
public String getFmobile() {
return fmobile;
}
public void setFmobile(String fmobile) {
this.fmobile = fmobile;
}
public String getFmessage() {
return fmessage;
}
public void setFmessage(String fmessage) {
this.fmessage = fmessage;
}
}
答案1
得分: 1
你之所以收到一个“空白”的活动,很可能是因为以下代码行的原因:
Query query = collectionReference.orderBy("Fname", Query.Direction.ASCENDING);
这意味着您试图查询FeedBacks
集合并根据Fname
属性对结果进行排序。这行代码让我想到您的数据库中可能有一个以大写字母开头的fname
属性,这是不正确的,因为您的Fmodel
类中的fname
属性以小写字母开头。为了使其正常工作,两个属性必须匹配。要解决此问题,请查看以下帖子中的我的答案:
英文:
You are getting a "blank" activity most likely because of the following lines of code:
Query query = collectionReference.orderBy ( "Fname", Query.Direction.ASCENDING );
Meaning that you are trying to query the FeedBacks
collection and order the results according to the Fname
property. That line of code makes me think that in your database you have the fname
property that starts with an upper case, which is not correct since the fname
property in your Fmodel
class starts with a lower case. To make it work, both properties must match. To solve this, please check my answer from the following post:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论