Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a recycler view adapter in Android Studio

Tags:

Can someone help me test this recycler view adapter? I wan't to make a local unit test, but doesn't really know how to begin? Following is my recyclerViewAdapter class:

 public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {     private List<TasksTask> task;     protected Context context;     public RecyclerViewAdapter(Context context, List<TasksTask> task) {         this.task = task;         this.context = context;     }     @Override     public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {         RecyclerViewHolders viewHolder = null;         View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list, parent, false);         viewHolder = new RecyclerViewHolders(layoutView, task);         return viewHolder;     }     @Override     public void onBindViewHolder(RecyclerViewHolders holder, int position) {         holder.categoryTitle.setText(task.get(position).getTask());      }     @Override     public int getItemCount() {         return this.task.size();     }} 

And here is my recyclerViewHolder class:

public class RecyclerViewHolders extends RecyclerView.ViewHolder {     private static final String TAG = RecyclerViewHolders.class.getSimpleName();     public TextView categoryTitle;     public ImageView deleteIcon;     private List<TasksTask> taskObject;       private FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();     private FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();     private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();       public RecyclerViewHolders(final View itemView, final List<TasksTask> taskObject) {         super(itemView);         this.taskObject = taskObject;         categoryTitle = (TextView) itemView.findViewById(R.id.task_title);         deleteIcon = (ImageView) itemView.findViewById(R.id.task_delete);          deleteIcon.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 String taskTitle = taskObject.get(getAdapterPosition()).getTask();                 mDatabase.child("Users").child(firebaseUser.getUid()).child("Tasks").child(taskTitle).removeValue();             }         });     } } 
like image 679
annabkj Avatar asked Apr 11 '17 12:04

annabkj


People also ask

How do I check my espresso RecyclerView?

To interact with RecyclerViews using Espresso, you can use the espresso-contrib package, which has a collection of RecyclerViewActions that can be used to scroll to positions or to perform actions on items: scrollTo() - Scrolls to the matched View, if it exists.

How do I use DiffUtil on Android?

Stay organized with collections Save and categorize content based on your preferences. DiffUtil is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second one. It can be used to calculate updates for a RecyclerView Adapter.

How check RecyclerView is empty in Android?

size() != 0) { NovaAdapter novaAdapter = new NovaAdapter(getActivity(),jsonList); recyclerView. setAdapter(novaAdapter); } else { // Show something like a dialog that the json list is 0 or do whatever you want... here the jsonlist have a count of 0 so it's empty! } } });


1 Answers

I've been wondering that myself and have found this: https://chelseatroy.com/2015/09/27/android-examples-a-test-driven-recyclerview/ there are example tests for each adapter method in there, hope that helps

like image 129
Sebastian Rüttger Avatar answered Sep 21 '22 13:09

Sebastian Rüttger