is it possible to combine FirestorRecyclerAdaper with pagination? - android

I am using FirestoreRecyclerAdaper with the normal form of retrieving and showing data.
my data becomes bigger and I want to make pagination in it, but I didn't find any tutorial to this point. I read documentations but nothing show how to do that because their is only one adapter and 2 queries and FirestorRecyclerAdaper accept only one query.
so is their a solution without changing my code?
final Query query = firebaseFirestore
.collection("Ads")
.orderBy("creationDate", Query.Direction.DESCENDING).limit(5);
query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
DocumentSnapshot lastVisible = queryDocumentSnapshots
.getDocuments().get(queryDocumentSnapshots.size() - 1);
Query next = firebaseFirestore.collection("Ads")
.orderBy("creationDate")
.startAfter(lastVisible)
.limit(10);
}
});
options = new FirestoreRecyclerOptions
.Builder<MyAdCard>()
.setQuery(query, MyAdCard.class)
.build();
dapter = new FirestoreRecyclerAdapter<MyAdCard, AllCardViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final AllCardViewHolder holder, final int position, #NonNull MyAdCard model) {
holder.publicAd_discription.setText(model.getMyAddiscriptiontxt());
}
#NonNull
#Override
public AllCardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview_categoryads, parent, false);
return new AllCardViewHolder(view);
}
};
categoryAdsRV.setAdapter(dapter);
private class AllCardViewHolder extends RecyclerView.ViewHolder {
public AllCardViewHolder(View itemView) {
super(itemView);
}
TextView publicAd_discription = itemView.findViewById(R.id.publicAd_Discription);
}
#Override
protected void onStart() {
super.onStart();
dapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if ( dapter != null ) dapter.stopListening();
}

There is nothing you can do here without changing your code significantly.
There is a FirestorePagingAdapter from Firebase-UI that may help you with the changes you'll need to make.
There is also a sample code I wrote with one example of using Android Architecture Components Paging along with both Firestore and Realtime Database.

Related

how to list person's post in his profile activity

I am building a social app using firebase , when it comes to profile activity and I want to post only person's post I can't ,I managed to post all users post to the home page as news feed but I can't filter only users posts , this is the users node with the person id as a child
i tried this
userRef=FirebaseDatabase.getInstance().getReference().child("Users");
RecieverId=getIntent().getExtras().get("brand_id").toString();
private void DisplayAllOffers() {
FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(PostRef.child(RecieverId),Posts.class).build();
FirebaseRecyclerAdapter<Posts,PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, PostViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull PostViewHolder holder, int position, #NonNull Posts model) {
// holder.Title.setText(model.getTitle());
final String postKey = getRef(position).getKey();
holder.Price.setText(model.getPrice());
holder.Duration.setText(model.getDuration());
holder.date.setText(model.getDate());
holder.description.setText(model.getDescription());
holder.time.setText(model.getTime());
holder.username.setText(model.getUsername());
holder.setLikeButtonStatus(postKey);
Picasso.get().load(model.getProfileImage()).into(holder.ProfileImage);
Picasso.get().load(model.getPostImage()).into(holder.postImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent postIntent = new Intent(BrandActivity.this,ClickPostActivity.class);
postIntent.putExtra("postKey",postKey);
startActivity(postIntent);
}
});
}
#NonNull
#Override
public PostViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view =LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.offer_layout,viewGroup,false);
PostViewHolder holder =new PostViewHolder(view);
return holder;
}
};
Profile_posts.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
and it returns an empty recyclerview
If you want to user-specific post than you have to change your options with
FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>().setQuery(PostRef.orderByChild("Uid").equalTo(FirebaseAuth.getInstance().getUid())),Posts.class).build();
here this PostRef.orderByChild("Uid").equalTo(FirebaseAuth.getInstance().getUid()) query filter curent user post
you also can get other than the current user specific post, all you have to do only change FirebaseAuth.getInstance().getUid() to user id in equalTo function

Why is firebase recycler adapter returning null

I am really stuck here, I need to get the Post class value that is held in users/user_id/latest_post only some users have this value. so I thought:
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
if (dataSnapshot.hasChild("latest_post")) {
Post latest = dataSnapshot.child("latest_post").getValue(Post.class);
latest_posts = latest;
post = latest;
}
}
should work, it does in getting me the values and ignoring the ones that don't contain a latest_posts child. But my recycler adapter and view-holder doesn't seem to use this and when I try setting up the recycler view is says the value it's trying to get is null.
FirebaseRecyclerOptions<Post> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Post>()
.setQuery(query, Post.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostHolder>(firebaseRecyclerOptions) {
#Override
protected void onBindViewHolder(#NonNull PostHolder postHolder, int position, #NonNull Post post) {
postHolder.setPost(post);
}
#Override
public PostHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_content, parent, false);
return new PostHolder(view);
}
};
Here is the rest of the firebase adapter ^ and then here is the setPost function:
void setPost(Post post) {
String key = post.getPost_id();
Integer reads = post.getReads();
String titleString = post.getTitle();
String bodyString = post.getBody();
String usernameString = post.getAuthor();
String category = post.getCategory();
System.out.println("setPost " + usernameString);
...
}
Top of file I am holding:
Post post;
which is updated with the values in the childEventListener. Any ideas where I am messing up? Thank you.
UPDATE:
I have made these changes using my User class which contains the Post class (latest_post):
void setupRecycler(Query query) {
query = FirebaseDatabase.getInstance()
.getReference()
.child("users");
FirebaseRecyclerOptions<User> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<User>()
.setQuery(query, User.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, PostHolder>(firebaseRecyclerOptions) {
#Override
protected void onBindViewHolder(#NonNull PostHolder postHolder, int position, #NonNull User user) {
if (user.getLatest_post().getAuthor() != null) {
postHolder.setPost(user);
}
}
#Override
public PostHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_content, parent, false);
return new PostHolder(view);
}
};
}
Now I am trying to filter when the setPost(user) is called so it only lets through if the latest_posts value is not null. But I must be misunderstanding something, because it gives me the first two names but still attempts to use the vaues which dont have latest_post which results in crashing due to null.
UPDATE 2:
Getting closer, this works:
query = FirebaseDatabase.getInstance()
.getReference()
.child("users").orderByChild("latest_post");
I'm guessing because it orders them by the users that have latest_post, but it crashes if I scroll to the bottom because Then all the rest are still there, so it attempts to set a string value to a textView when it is null. How do I exclude these pesky things?
It was because you can't filter a query to exclude posts that do not exist, and you do not have access to the list that firebaseRecyclerAdapter uses. The only solution to accurately and safely filter the data the way I needed was to use a custom recyclerView Adapter, and a list of my class, and filter and order the list as needed after the query.

FirestoreRecyclerAdapter - how do I know when data has been retrieved?

I'm retrieving data using a FirestoreRecyclerAdapter, and, on completion, I need to check whether any items have been retrieved or not. I can't figure out how to do this.
I'm calling it from a class called FragmentChartsList, shown below. This should set up the adapter initially, with "name" as the value for mOrder. Later, the Activity which contains this Fragment can call setOrderField() with a different value of mOrder, which the user has selected from a Spinner.
Each time setOrderField() is called, a new adapter instance is created and attached to the recyclerView. At this point I need to check whether the new version of the adapter contains any data, and either show a "no Charts found" message, or show the Charts which were retrieved (obviously if the list is just being sorted, then the number of items remains the same, but I'm going to be expanding this to allow the user to filter the Charts by different criteria, so the number of Charts returned will change).
Currently, setOrderField() calls refreshViewOnNewData(), which should find out how many Charts are being shown; if it's 0, it should show the "no Charts found" message, and if it's >0 it should show the RecyclerView containing the Charts.
At the moment, I'm always getting a value of 0 when I try to count the Charts. I suspect it's because the adapter hasn't finished retrieving them from the database yet, but I can't find anything that allows me to add some kind of "onComplete" listener so that I know it's finished.
Can anyone suggest how I can achieve this?
public abstract class FragmentChartsList extends Fragment {
private FirebaseFirestore mDatabaseRef;
private ChartListAdapter mAdapter;
private Query mChartsQuery;
private RecyclerView mRecycler;
private String mOrder = "name";
private TextView mLoadingList, mEmptyList;
public FragmentChartsList() {}
#Override
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(
R.layout.fragment_charts_list, container, false);
mRecycler = rootView.findViewById(R.id.charts_list);
mRecycler.setHasFixedSize(true);
mLoadingList = rootView.findViewById(R.id.loading_list);
mEmptyList = rootView.findViewById(R.id.empty_list);
// Set up Layout Manager, and set Recycler View to use it
LinearLayoutManager mManager = new LinearLayoutManager(getActivity());
mManager.setReverseLayout(true);
mManager.setStackFromEnd(true);
mRecycler.setLayoutManager(mManager);
// Connect to the database
mDatabaseRef = FirebaseFirestore.getInstance();
setOrderField(mOrder); // Initialised to "name"
return rootView;
}
#Override
public void onStart() {
super.onStart();
mAdapter.startListening();
}
#Override
public void onStop() {
super.onStop();
mAdapter.stopListening();
}
#Override
public void onDestroy() {
super.onDestroy();
mAdapter.stopListening();
}
// HELPER FUNCTIONS
public void setOrderField(String order) {
mOrder = order;
mChartsQuery = getQuery(mDatabaseRef, mOrder);
// Update recycler options
FirestoreRecyclerOptions<Chart> recyclerOptions = new FirestoreRecyclerOptions.Builder<Chart>()
.setQuery(mChartsQuery, Chart.class)
.build();
mAdapter = new ChartListAdapter(recyclerOptions, getActivity());
mAdapter.startListening();
mRecycler.swapAdapter(mAdapter, true);
refreshViewOnNewData();
}
private void refreshViewOnNewData() {
// Hide "loading" text
mLoadingList.setVisibility(View.GONE);
// Check number of charts being shown
//if (mAdapter != null && (mAdapter.getCount() > 0)) {
// If > 0, show Charts
mEmptyList.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
} else {
// If number of Charts = 0
// show "no charts"
mEmptyList.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
}
The adapter class looks like this:
public class ChartListAdapter extends FirestoreRecyclerAdapter<Chart, ChartViewHolder> {
private Activity mActivity;
private int mCount;
public ChartListAdapter(FirestoreRecyclerOptions<Chart> recyclerOptions, Activity activity) {
super(recyclerOptions);
mActivity = activity;
}
#Override
protected void onBindViewHolder(#NonNull ChartViewHolder holder, int position, #NonNull Chart model) {
final String chartKey = this.getSnapshots().getSnapshot(position).getId();
model.setKey(chartKey);
// Set click listener for the chart
// On click, the user can view the chart
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mActivity, ActivityViewChart.class);
intent.putExtra("ChartKey", chartKey);
mActivity.startActivity(intent);
}
});
// Implement long-click menu
mActivity.registerForContextMenu(holder.itemView);
// Bind Chart to ViewHolder
holder.bindToChart(model);
}
#NonNull
#Override
public ChartViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chart, parent, false);
return new ChartViewHolder(view);
}
#Override
public void onDataChanged() {
super.onDataChanged();
mCount = getItemCount();
}
public int getCount() {
return mCount;
}
}
Figured this out... I needed to set a listener on the query instead.
So, instead of having the call to refreshViewOnNewData from setOrder above, I now have:
mChartsQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot queryDocumentSnapshots, #Nullable FirebaseFirestoreException e) {
if (queryDocumentSnapshots != null) {
mLoadingList.setVisibility(View.GONE);
if(queryDocumentSnapshots.size() > 0) {
mEmptyList.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
}else {
mEmptyList.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
}
});
}
Also removed mCount from the adapter class, along with getCount and onDataChanged

How to implement pagination in firestore specially in firebase official Friendly eats app? [duplicate]

I read Firestore documentation and all articles on internet(stackoverflow) about Firestore pagination but no luck. I tried to implement the exact code in docs, but nothing happens. I have a basic database with items(over 1250 or more) and I want to get them progressively. By scrolling to load 15 items (to the last item in the database).
If using docs code:
// Construct query for first 25 cities, ordered by population
Query first = db.collection("cities")
.orderBy("population")
.limit(25);
first.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot documentSnapshots) {
// ...
// Get the last visible document
DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
.get(documentSnapshots.size() -1);
// Construct a new query starting at this document,
// get the next 25 cities.
Query next = db.collection("cities")
.orderBy("population")
.startAfter(lastVisible)
.limit(25);
// Use the query for pagination
// ...
}
});
How to do? Documentation has not too many details.
PS: I need with recycler view (not list view) when user scrolls. Thanks
As it is mentioned in the official documentation, the key for solving this problem is to use the startAfter() method. So you can paginate queries by combining query cursors with the limit() method. You'll be able to use the last document in a batch as the start of a cursor for the next batch.
To solve this pagination problem, please see my answer from this post, in which I have explained step by step, how you can load data from a Cloud Firestore database in smaller chunks and display it in a ListView on button click.
Solution:
To get the data from your Firestore database and display it in smaller chunks in a RecyclerView, please follow the steps below.
Let's take the above example in which I have used products. You can use products, cities or whatever you want. The principles are the same. Assuming that you want to load more products when user scrolls, I'll use RecyclerView.OnScrollListener.
Let's define first the RecyclerView, set the layout manager to LinearLayoutManager and create a list. We also instantiate the adapter using the empty list and set the adapter to our RecyclerView:
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<ProductModel> list = new ArrayList<>();
ProductAdapter productAdapter = new ProductAdapter(list);
recyclerView.setAdapter(productAdapter);
Let's assume we have a database structure that looks like this:
Firestore-root
|
--- products (collection)
|
--- productId (document)
|
--- productName: "Product Name"
And a model class that looks like this:
public class ProductModel {
private String productName;
public ProductModel() {}
public ProductModel(String productName) {this.productName = productName;}
public String getProductName() {return productName;}
}
This how the adapter class should look like:
private class ProductAdapter extends RecyclerView.Adapter<ProductViewHolder> {
private List<ProductModel> list;
ProductAdapter(List<ProductModel> list) {
this.list = list;
}
#NonNull
#Override
public ProductViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
return new ProductViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ProductViewHolder productViewHolder, int position) {
String productName = list.get(position).getProductName();
productViewHolder.setProductName(productName);
}
#Override
public int getItemCount() {
return list.size();
}
}
The item_product layout contains only one view, a TextView.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view"
android:textSize="25sp"/>
And this is how the holder class should look like:
private class ProductViewHolder extends RecyclerView.ViewHolder {
private View view;
ProductViewHolder(View itemView) {
super(itemView);
view = itemView;
}
void setProductName(String productName) {
TextView textView = view.findViewById(R.id.text_view);
textView.setText(productName);
}
}
Now, let's define a limit as a global variable and set it to 15.
private int limit = 15;
Let's define now the query using this limit:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference productsRef = rootRef.collection("products");
Query query = productsRef.orderBy("productName", Query.Direction.ASCENDING).limit(limit);
Here is the code that also does the magic in your case:
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
ProductModel productModel = document.toObject(ProductModel.class);
list.add(productModel);
}
productAdapter.notifyDataSetChanged();
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
isScrolling = false;
Query nextQuery = productsRef.orderBy("productName", Query.Direction.ASCENDING).startAfter(lastVisible).limit(limit);
nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> t) {
if (t.isSuccessful()) {
for (DocumentSnapshot d : t.getResult()) {
ProductModel productModel = d.toObject(ProductModel.class);
list.add(productModel);
}
productAdapter.notifyDataSetChanged();
lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
if (t.getResult().size() < limit) {
isLastItemReached = true;
}
}
}
});
}
}
};
recyclerView.addOnScrollListener(onScrollListener);
}
}
});
In which lastVisible is a DocumentSnapshot object which represents the last visible item from the query. In this case, every 15'th one and it is declared as a global variable:
private DocumentSnapshot lastVisible;
And isScrolling and isLastItemReached are also global variables and are declared as:
private boolean isScrolling = false;
private boolean isLastItemReached = false;
If you want to get data in realtime, then instead of using a get() call you need to use addSnapshotListener() as explained in the official documentation regarding listening to multiple documents in a collection. More information you can find the following article:
How to create a clean Firestore pagination with real-time updates?
FirebaseUI-Android also recently came out with a Firestore Paginator.
I have used it in my code, and it works great - just keep in mind that it operates using .get() instead of .addSnapshotListener(), so the recycler is not in realtime.
See the docs here:
https://github.com/firebase/FirebaseUI-Android/tree/master/firestore#using-the-firestorepagingadapter
You can also use FirestorePagingAdapter provided by Firebase-UI-Firestore
You need to install this dependency
implementation 'com.firebaseui:firebase-ui-firestore:latest_version_here'
Solution
Step 1: Create a global Firestore paging adapter variable and pass the Model class and ViewHolder, and also the Model variable.
private FirestorePagingAdapter<Model, ModelViewHolder> adapter;
private Model model;
Step 2:
Create a firebase query
Query query = db.collection("cities")
.orderBy("population");
Step 3:
Let's build the pagedlist config. Here you will pass how much data to be queried in each page;
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(10)
.setPageSize(15)
.build();
Step 4: After setting the config, let's now build the Firestore paging options where you will pass the query and config.
FirestorePagingOptions<Model> options = new FirestorePagingOptions.Builder<Model>()
.setLifecycleOwner(this)
.setQuery(query, config, snapshot -> {
model = snapshot.toObject(Model.class);
return model;
})
.build();
Step: 5
Now let's pass the data to the Recylerview
adapter = new FirestorePagingAdapter<Model, ModelViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ModelViewHolder holder, int position, #NonNull Model model) {
holder.bindTO(model);
}
#NonNull
#Override
public ModelViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_model, parent, false);
return new ModelViewHolder(view);
}
#Override
protected void onError(#NonNull Exception e) {
super.onError(e);
//logic here
}
#Override
protected void onLoadingStateChanged(#NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
break;
case LOADING_MORE:
break;
case LOADED:
notifyDataSetChanged();
break;
case ERROR:
Toast.makeText(requireActivity(), "Error", Toast.LENGTH_SHORT).show();
//logic here
break;
case FINISHED:
//logic here
break;
}
}
};
productRecycler.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
Happy Coding!

FirebaseRecyclerAdapter add a hasChild() method?

Here's my database:
Database:{
X:{
JK-KDUKSIDKSIIJDSL1:{
text:"hello",
name:"Donald J. Drunk"
}
JK-KDadDFDDIJDSL1:{
name:"Killery Hlinton"
}
}
}
And I want to filter my FirebaseRecyclerAdapter such that only add to my RecyclerView data that contains the key text. How is this possible? How can I add it? Current code:
mAdapter = new FirebaseRecyclerAdapter<Campaign, CampaignHolder>(Campaign.class, R.layout.recyclerview_template, CampaignHolder.class, ref) {
#Override
public void populateViewHolder(final CampaignHolder viewHolder, final Campaign campaign, final int position) {
findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);
MainActivity.this.holder = viewHolder;
viewHolder.setTitle(campaign.title);
//... Other "set" methods
}
}
You could try overriding the onBindViewHolder method in your FirebaseRecyclerAdapter. Something like this:
mAdapter = new FirebaseRecyclerAdapter<Campaign, CampaignHolder>(Campaign.class, R.layout.recyclerview_template, CampaignHolder.class, ref) {
#Override
public void onBindViewHolder(CampaignHolder viewHolder, int position) {
Campaign model = getItem(position);
if(model.getText() != null){
populateViewHolder(viewHolder, model, position);
}
}
#Override
public void populateViewHolder(final CampaignHolder viewHolder, final Campaign campaign, final int position) {
findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);
MainActivity.this.holder = viewHolder;
viewHolder.setTitle(campaign.title);
//... Other "set" methods
}
};
With this approach you would still pull all data at the query location but entries without a text attribute just wouldn't be displayed. If that is unsatisfactory and you want to minimize the amount of data you download per query then the only other option I see is closer to what is mentioned in the comments above - meaning you would need to create a separate location that only stores those entries which have a text value.

Categories

Resources