private static ArrayList<String> playerList = new ArrayList<String>();
在你的处理程序方法:
if(playerList.size() < 10) { // playerList.add } else { // do nothing }
编辑:你的错误在这里:
if(playerList.size() < 10) { Button confirm = (Button) findViewById(R.id.add); confirm.setOnClickListener(new OnClickListener() { public void onClick(View v) { EditText playername = (EditText) findViewById(R.id.userinput); playerList.add(playername.getText().toString()); adapter.notifyDataSetChanged(); playername.setText(""); }}); } else { // do nothing }
您应该检查onClickListener内部的大小,而不是外部:
Button confirm = (Button) findViewById(R.id.add); confirm.setOnClickListener(new OnClickListener() { public void onClick(View v) { EditText playername = (EditText) findViewById(R.id.userinput); if(playerList.size() < 10) { playerList.add(playername.getText().toString()); adapter.notifyDataSetChanged(); playername.setText(""); } else { // do nothing } } });
翻译自:https://stackoverflow.com/questions/8969805/arraylist-limit-to-hold-10-values