How do I limit the max number of items that can be added in this listview to 10?

so I am working on a list, and I want the max number of items that the user can put into the list to be 10. But, I am not able to understand how I can do this here… I don’t want the list to be infinite. I tried a lot of things like loops, and stuff, but I was having trouble implementing it. What is the most efficient solution for this problem?

This is the code for my adapter :

// full subtask adapter code

package com.example.taskmasterv3;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class SubtaskAdapter extends ArrayAdapter<subtask> {

    private final Context context;
    private ArrayList<subtask> values;

    public SubtaskAdapter(Context context, ArrayList<subtask> list) {

        //since your are using custom view,pass zero and inflate the custom view by overriding getview

        super(context, 0 , list);
        this.context = context;
        this.values = list;

    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        //check if its null, if so inflate it, else simply reuse it
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.subtask_item, parent, false);

        //use convertView to refer the childviews to populate it with data
        TextView tvSubtaskName = convertView.findViewById(;
        ImageView ivPri = convertView.findViewById(;
        ImageView ivTime = convertView.findViewById(;
        ImageView ivDelete = convertView.findViewById(;


        ivDelete.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            int position = (Integer) v.getTag();


        if (values.get(position).isPriHigh()) {
        } else if (values.get(position).isPriMed()) {
        } else if (values.get(position).isPriLow()) {

        if (values.get(position).isTimeMore()) {
        } else if (values.get(position).isTimeMed()) {
        } else if (values.get(position).isTimeLess()) {

        // Delete button for subtasks (NOT WORKING)

        //return the view you inflated
        return convertView;

    //to keep adding the new subtasks try the following
    public void addANewSubTask(subtask newSubTask){
        ArrayList<subtask> newvalues = new ArrayList<>(this.values);
        this.values = newvalues;




Just override getCount() in your adapter

public int getCount() {
        return 10;
    else {
        return super.getCount();