Thiru Thiru - 9 months ago 28
Android Question

On scroll in recyclerview the json retrieving data missing

I created a RecyclerView with retrieve json data with cardview. The problem is carview third textview data missing after scrolling? What is the problem the third textview missing. How to solve this?

public class RecycleCardDirectoryAdapter extends RecyclerView.Adapter
<RecycleCardDirectoryAdapter.MyViewHolder> {

private final ArrayList<DirectoryModel> mDirectoryModelArrayList;
public final ArrayList<ArrayList<String>> mCategoryIcon;
private final WeakReference<Context> mContextWeakReference;
private Context context;
private ImageView IvCollection0, IvCollection1, IvCollection2, IvCollection3, IvCollection4, IvCollection5;
private String categoryIcon0;
private DisplayImageOptions options;
private ProgressBar progressBar;

public RecycleCardDirectoryAdapter(ArrayList<DirectoryModel> data, ArrayList<ArrayList<String>> icon, Context context) {
this.mDirectoryModelArrayList = data;
this.mCategoryIcon = icon;
this.mContextWeakReference = new WeakReference<>(context);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
context = mContextWeakReference.get();
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_cardview_directory, parent, false);
return new MyViewHolder(view, context, mDirectoryModelArrayList);
}

@Override
public void onBindViewHolder( MyViewHolder holder,int listPosition) {

TextView TvName = holder.mTvName;
TextView TvPhone = holder.mTvPhone;
TextView TvEmail = holder.mTvEmail;
TextView TvWeb = holder.mTvWeb;
ImageView IvIcon = holder.mIvIcon;

IvCollection0 = holder.mIvCollection;
IvCollection1 = holder.mIvCollection1;
IvCollection2 = holder.mIvCollection2;
IvCollection3 = holder.mIvCollection3;
IvCollection4 = holder.mIvCollection4;
IvCollection5 = holder.mIvCollection5;

progressBar = holder.mProgressBar;

options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();

String receivedImage = context.getResources().getString(R.string.WS_DIRECTORY_IMAGE) +
mDirectoryModelArrayList.get(listPosition).getCompanyImage();



String receivedName = mDirectoryModelArrayList.get(listPosition).getCompanyFirstName();
String receivedContact = mDirectoryModelArrayList.get(listPosition).getCompanyMobile();
String receivedEmail = mDirectoryModelArrayList.get(listPosition).getCompanyEmail();
String receivedWeb = mDirectoryModelArrayList.get(listPosition).getCompanyWebsite();

ImageLoader.getInstance().displayImage(receivedImage, IvIcon, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}

@Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}
});

if (receivedContact.equals("")) {
TvPhone.setText(R.string.lbl_null);
}
if (receivedEmail.equals("")) {
TvEmail.setVisibility(View.INVISIBLE);
}
if (receivedWeb.equals("")) {
TvWeb.setVisibility(View.INVISIBLE);
}

TvName.setText(receivedName);
TvPhone.setText(receivedContact);
TvEmail.setText(receivedEmail);
TvWeb.setText(receivedWeb);
}

@Override
public int getItemCount() {
return mDirectoryModelArrayList.size();
}

static class MyViewHolder extends RecyclerView.ViewHolder {

private final TextView mTvName;
private final TextView mTvPhone;
private final TextView mTvEmail;
private final TextView mTvWeb;
private final ImageView mIvIcon, mIvCollection, mIvCollection1, mIvCollection2,
mIvCollection3, mIvCollection4, mIvCollection5;
private final ProgressBar mProgressBar;
private TinyDB mPrefDb;
public static final String COMPANY_ID = "companyId";

MyViewHolder(View itemView, final Context context, final ArrayList<DirectoryModel> mDirectoryModelArrayList) {
super(itemView);
this.mProgressBar = (ProgressBar) itemView.findViewById(R.id.progressBar_directory);
this.mTvName = (TextView) itemView.findViewById(R.id.tv_company);
this.mTvPhone = (TextView) itemView.findViewById(R.id.tv_ph_number);
this.mTvEmail = (TextView) itemView.findViewById(R.id.tv_email);
this.mTvWeb = (TextView) itemView.findViewById(R.id.tv_website);
this.mIvIcon = (ImageView) itemView.findViewById(R.id.img_icon);

this.mIvCollection = (ImageView) itemView.findViewById(R.id.img_collections);
this.mIvCollection1 = (ImageView) itemView.findViewById(R.id.img_collections1);
this.mIvCollection2 = (ImageView) itemView.findViewById(R.id.img_collections2);
this.mIvCollection3 = (ImageView) itemView.findViewById(R.id.img_collections3);
this.mIvCollection4 = (ImageView) itemView.findViewById(R.id.img_collections4);
this.mIvCollection5 = (ImageView) itemView.findViewById(R.id.img_collections5);

mPrefDb = new TinyDB(context);

LinearLayout ll = (LinearLayout) itemView.findViewById(R.id.ll_directory_layout);
ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPrefDb.putInt(COMPANY_ID, mDirectoryModelArrayList.get(getAdapterPosition()).getCompanyID());
Intent intent = new Intent(context,CompanyInformationActivity.class);
context.startActivity(intent);
}
});
}
}
}


In this adapter tvWeb textview data are missing in while scrolling...

Answer Source
    if (receivedEmail.equals("")) {
                TvEmail.setVisibility(View.INVISIBLE);
            }else{
                TvEmail.setVisibility(View.VISIBLE);
}
             if (receivedWeb.equals("")) {
                TvWeb.setVisibility(View.INVISIBLE);
             }else{
                TvWeb.setVisibility(View.VISIBLE);
}

this happens when you set some views invisible and do not set them visible.The adapter will load items again when scrolled and will misbehave if you do not set visibility of items to visible which were setĀ invisible. do this for all the views that are hidden if value is not found in the retrived data.