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;
    }





    @Override
    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(R.id.tvSubtaskName);
        ImageView ivPri = convertView.findViewById(R.id.ivPri);
        ImageView ivTime = convertView.findViewById(R.id.ivTime);
        ImageView ivDelete = convertView.findViewById(R.id.ivDelete);

        ivDelete.setTag(position);














        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            int position = (Integer) v.getTag();
            remove(values.remove(position));
            notifyDataSetChanged();
            }
        });



        tvSubtaskName.setText(values.get(position).getSubtaskName());

        if (values.get(position).isPriHigh()) {
            ivPri.setImageResource(R.drawable.priority_high);
        } else if (values.get(position).isPriMed()) {
            ivPri.setImageResource(R.drawable.priority_med);
        } else if (values.get(position).isPriLow()) {
            ivPri.setImageResource(R.drawable.priority_low);
        }

        if (values.get(position).isTimeMore()) {
            ivTime.setImageResource(R.drawable.time_symbol_more);
        } else if (values.get(position).isTimeMed()) {
            ivTime.setImageResource(R.drawable.time_symbol_med);
        } else if (values.get(position).isTimeLess()) {
            ivTime.setImageResource(R.drawable.time_symbol_less);
        }


        // 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);
        newvalues.add(newSubTask);
        this.values = newvalues;
        notifyDataSetChanged();

    }

}

Answer

Just override getCount() in your adapter

@Override
public int getCount() {
    if(this.values.size()>10)
    {
        return 10;
    }
    else {
        return super.getCount();
    }
}