Prabh Prabh - 1 month ago 21
Android Question

RecyclerView not displaying data on cardview properly (Android)?

I am making a file manager and I am trying to use RecyclerView. The problem is recyclerview is not displaying data properly on cardview. It is showing random data and sometimes same data on different cardviews. Some cardviews are empty that is not showing any data. I am also not saving state of recyclerview. The data is repeating randomly. I am using recyclerview in fragment. Is there a way to fix this? I am newbie dev in android.

public class FileInternalStorageRecyclerAdapter extends RecyclerView.Adapter<FileInternalStorageRecyclerAdapter.ViewHolder>{
Context context;
ArrayList<InternalStorageModel> fileList;
InternalStorageFragment.InternalStorageListener listener;
public FileInternalStorageRecyclerAdapter(Context context,ArrayList<InternalStorageModel> fileList, InternalStorageFragment.InternalStorageListener listener){
this.fileList=fileList;
this.listener=listener;
this.context=context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardView cardView=(CardView) LayoutInflater.from(context).inflate(R.layout.file_internal_storage_recycler_adapter,parent,false);
return new ViewHolder(cardView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final CardView cv=holder.cardView;
final InternalStorageModel model=fileList.get(position);
if (model.fileIsDirectory){
if (model.fileName.startsWith(".")){
holder.imgLogo.setImageResource(R.drawable.ic_hidden_folder_36dp);
holder.txtName.setText(model.fileSimpleName+" ("+model.fileCountInDirectory+" )");
return;
}
holder.imgLogo.setImageResource(R.drawable.ic_folder_36dp);
holder.txtName.setText(model.fileSimpleName+" ("+model.fileCountInDirectory+" )");

}else if (model.fileIsFile){
if (model.fileName.startsWith(".")){
holder.imgLogo.setImageResource(R.drawable.ic_hidden_file_36dp);
holder.txtName.setText(model.fileSimpleName);
return;
}
holder.imgLogo.setImageResource(R.drawable.ic_file_36dp);
holder.txtName.setText(model.fileSimpleName);

}
cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
if (model.fileIsDirectory){
listener.navigationThroughFragment(model.fileAbsolutePath);

}else if (model.fileIsFile){
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(model.fileAbsoluteFile),"*/*");
context.startActivity(Intent.createChooser(intent,"Complete action using..."));
}
}catch(Exception e){
e.printStackTrace();
}
}
});

}

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

class ViewHolder extends RecyclerView.ViewHolder{
private ImageView imgLogo;
private TextView txtName;
private CardView cardView;
private ViewHolder(CardView itemView) {
super(itemView);
cardView=itemView;
imgLogo=(ImageView)itemView.findViewById(R.id.imgLogo);
txtName=(TextView)itemView.findViewById(R.id.txtName);
}
}


}

Answer

What I think is there must be a case when the model item is not a file and not a directory (symlink?) and that is where you get into trouble.

The Adapter must re-bind the data for every getView() call, otherwise you will see irrelevant previous data. Try adding another else block where the model is not a file and not a directory.

    // your code as it was
    // ...
} else if (model.fileIsFile) {
    // ...
    // your code as it was
    // ...
} else {
    holder.imgLogo.setImageResource(R.drawable.something);
    holder.txtName.setText(model.fileSimpleName);
}
Comments