RecyclerView在活动中始终是空白的。

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

RecyclerView is always blank in activity

问题

当我运行我的应用程序时,RecyclerView 在活动中总是显示为空白。我使用了普通的RecyclerViewFirebaseAdapter,但列表没有显示任何数据。数据在实时数据库和云 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 &lt;Fmodel,FeedsAdapter.myFeedViewHolder&gt; {
public FeedsAdapter(@NonNull FirestoreRecyclerOptions &lt;Fmodel&gt; 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 ( &quot;FeedBacks&quot; );
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 ( &quot;Fname&quot;, Query.Direction.ASCENDING );
FirestoreRecyclerOptions &lt;Fmodel&gt; options = new FirestoreRecyclerOptions.Builder&lt;Fmodel&gt;().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, &quot;fetch on&quot;, Toast.LENGTH_SHORT ).show ( );
}
@Override
protected void onStop() {
super.onStop ( );
feedsAdapter.stopListening ();
Toast.makeText ( this, &quot;fetch off&quot;, 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属性以小写字母开头。为了使其正常工作,两个属性必须匹配。要解决此问题,请查看以下帖子中的我的答案:

https://stackoverflow.com/questions/53924189/how-to-read-firestore-sub-collection-and-pass-it-to-firestorerecycleroptions/53943559#53943559

英文:

You are getting a "blank" activity most likely because of the following lines of code:

Query query = collectionReference.orderBy ( &quot;Fname&quot;, 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:

> https://stackoverflow.com/questions/53924189/how-to-read-firestore-sub-collection-and-pass-it-to-firestorerecycleroptions/53943559#53943559

huangapple
  • 本文由 发表于 2020年8月13日 21:47:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63396619.html
匿名

发表评论

匿名网友

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

确定