【问题标题】:How do I limit the max number of items that can be added in this listview to 10?如何将此列表视图中可以添加的最大项目数限制为 10?
【发布时间】:2021-07-10 02:53:58
【问题描述】:

所以我正在处理一个列表,我希望用户可以放入列表中的最大项目数为 10。但是,我无法理解如何在这里做到这一点......我不不希望列表是无限的。我尝试了很多诸如循环之类的东西,但我在实现它时遇到了麻烦。解决这个问题最有效的方法是什么?

这是我的适配器的代码:

// 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();

    }

}

【问题讨论】:

  • 您可以根据需要设置数组限制。
  • 我建议你添加 recyclerView 作为比较列表视图

标签: java android listview


【解决方案1】:

只需覆盖适配器中的 getCount()

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

【讨论】:

    【解决方案2】:

    你可以试试这样的

    if(newvalues.size() < 10) {
         newvalues.add(newsubtask)
    } else {
       // dont add values, show user the size is 10
    }
    

    【讨论】:

    • 好的,我试过了: if (newvalues.size()
    • 你添加了 if(newvalues.size()
    • 试过了。还是同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2018-09-06
    相关资源
    最近更新 更多