Reputation: 131
I have a list of the mobile contacts inside a RecyclerView also I have an online database with registered mobile numbers I made a web service to check if the number is registered or not and it's working.
what I want to do is there is an invite text in the item on the right and I want to make it visible for unregistered users and registered users will be invisible for them this is my onBindViewHolder below
what I am getting now is the invite text keeps showing and disappearing and then keeps visible for all
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
Log.d(TAG, "Element " + position + " set.");
// Get element from your dataset at this position and replace the contents of the view
// with that element
viewHolder.getTextView().setText(mDataSet[position]);
viewHolder.getTextView2().setText(mDataSet2[position]);
viewHolder.getImageView().setImageResource(mDataSet3[position]);
Phone phone=new Phone();
phone.phone=mDataSet2[position];
WebService.getInstance().getApi().checkNumber(phone).enqueue(new Callback<MainResponse>() {
@Override
public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
if (response.body().status == 1){
viewHolder.getTextViewInvite().setVisibility(View.GONE);
}else {
viewHolder.getTextViewInvite().setVisibility(View.VISIBLE);
}
}
@Override
public void onFailure(Call<MainResponse> call, Throwable t) {
viewHolder.getTextViewInvite().setVisibility(View.VISIBLE);
}
});
}
Upvotes: 0
Views: 82
Reputation: 52
As you scroll in the recyclerview, you are calling the api which will definitely take some time to respond and so your view shows and hides respectively.
Cons:
Unnecessary network calls on scrolling.
Ideally, you should prefetch the status of all users as you load contacts and not on scrolling. If you like the approach, then do accept this and implement.
Upvotes: 1
Reputation: 1936
The reason is that your Webservice is called as an async task, that means your code does not wait for an answer from web service, and when Webservice gives an answer, your code will continue. but it is too late because your recycled view adapter bound all view objects, so try setting boolean in your phone object and before you give phone list to your recycle view check if each one is registered or not and set the boolean true or false.
Upvotes: 0