Reputation: 247
In my application i want add some list from server into my recyclerView
.
My list items size has 10, but i want add last 7 item to this RecyclerView. I don't want add first 3 item to this recyclerView
I write below code :
Call<ListCartResponse> call = apIs.getListCardResponse(jwtToken);
call.enqueue(new Callback<ListCartResponse>() {
@Override
public void onResponse(Call<ListCartResponse> call, Response<ListCartResponse> response) {
if (response.body() != null) {
if (response.body().getRes().getCarts() != null) {
if (response.body().getRes().getCarts().size() > 0) {
model.clear();
model.addAll(response.body().getRes().getCarts());
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<ListCartResponse> call, Throwable t) {
}
});
But in my above codes add all of 10 items to recyclerView
.
I want add just last 7 items and remove 3 first items.
My recyclerView items now : 1 2 3 4 5 6 7 8 9 10 But i want : 4 5 6 7 8 9 10
How can i it?
Upvotes: 1
Views: 409
Reputation: 12118
You can use subList()
method of List
interface:
For your example :
model.clear();
model.addAll(response.body().getRes().getCarts().subList(3, 9));
Note: Beware that if your list from server is less than 10 items and you still need last 7 items, then you should pass index dynamically instead (Above solution will crash for that instance)
subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified
fromIndex
, inclusive, andtoIndex
, exclusive. (IffromIndex
andtoIndex
are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
list.subList(from, to).clear();
Similar idioms may be constructed for
indexOf
andlastIndexOf
, and all of the algorithms in theCollections
class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
Parameters:
fromIndex - low endpoint (inclusive) of the subList
toIndex - high endpoint (exclusive) of the subList
Returns:
a view of the specified range within this list
Upvotes: 2
Reputation: 22832
I think it is because of the model
in response part does not refer to model
in your adapter class. Add this to your SupportListAdapter
class, the use it when response has received:
public void replaceModelList(List<Res> newModel){
model.clear();
model.addAll(newModel);
notifyDataSetChanged();
}
When result has received:
if (response.body().getRes().getCarts() != null) {
if (response.body().getRes().getCarts().size() > 0)
adapter.replaceModelList(response.body().getRes().getCarts());
Upvotes: 0
Reputation: 53
Add this:
if (response.body().getRes().getCarts().size() > 0)
{
model.clear();
model.addAll(response.body().getRes().getCarts());
model.remove(0);
model.remove(0);
model.remove(0);
adapter.notifyDataSetChanged();
}
Upvotes: 1