Closed cristianhoyos66-zz closed 8 years ago
Thanks for your question!
Generally, I'm not sure if you really have to call new ServicesAdapter
and rvPendingServices.setAdapter
every time, regardless of this library. Often, you can just add items to your existing adapter or the list that backs the adapter and the UI will update automatically.
You only get data when you have subscribed first, right?
And thus the SubscribeListener
should do what you need. Whenever you subscribe to data, that listener should be fired when the initial batch of data has been sent completely. The usage is as documented in the README:
mMeteor.subscribe("my-subscription", new Object[] { arg1, arg2 }, new SubscribeListener() { });
Can you try this?
Thanks for aswering,
I thought SubscribeListener
but new SubscribeListener() { }
has these methods:
mMeteor.subscribe("my-subscription", new Object[] { arg1, arg2 }, new SubscribeListener() {
@Override
public void onSuccess() {
}
@Override
public void onError(String s, String s1, String s2) {
}
});
and onSuccess
does not return anything and on the documentatio I don't find how to get the first data with SubscribeListener
@cristianhoyos66 The idea was rather something like this:
private List<Service> mServiceList;
private ServicesAdapter mServicesAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
mServiceList = new ArrayList<Service>();
mServicesAdapter = new ServicesAdapter(mServiceList);
rvPendingServices.setAdapter(mServicesAdapter);
// subscribe to get the entries
mMeteor.subscribe("my-subscription", new Object[] { arg1, arg2 }, new SubscribeListener() {
@Override
public void onSuccess() {
// all entries have been received and thus we can show them now
mServicesAdapter.notifyDataSetChanged();
}
@Override
public void onError(String s, String s1, String s2) {
System.out.println("There was an error: "+s+", "+s1+", "+s2);
}
});
// entries start being sent now and will be received in `onDataAdded` ...
}
@Override
public void onDataAdded(String collectionName, String documentID, String fieldsJson) {
try {
JSONObject obj = new JSONObject(fieldsJson);
String title = obj.getString("title");
Service service = new Service(title);
mServiceList.add(service);
}
catch (JSONException e) {
e.printStackTrace();
}
}
Does that make sense?
@mwaclawek Thank you so much, you are helping me with this.
I'm sorry if this is a dumb question, but I don't get with a good solution.
I'm doing this:
rvPendingServices is a RecyclerView that is setted with an adapter and one List. The problem here is that i'm calling several times rvPendingServices.setAdapter(adapter); because I dont know when onDataAdded get the last document on fieldsJson var.
I'm also looking for a way that I can get collectionSize at least, to do a counter and to compare collectionSize with counter and when these are equal to can call rvPendingServices.setAdapter(adapter); but I don't find anything
Thank you.