Android:ArrayList中的索引越界异常
我正在使用阵列适配器在列表视图中显示阵列列表。我可以添加和删除项目。假设在列表视图中并没有任何项,若我选择删除它的显示索引绑定外异常。我所需要的是它应该显示吐司像“没有项目删除”。澄清我专家!!我的代码如下:Android:ArrayList中的索引越界异常,android,indexoutofboundsexception,Android,Indexoutofboundsexception,我正在使用阵列适配器在列表视图中显示阵列列表。我可以添加和删除项目。假设在列表视图中并没有任何项,若我选择删除它的显示索引绑定外异常。我所需要的是它应该显示吐司像“没有项目删除”。澄清我专家!!我的代码如下: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bAdd =
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bAdd = (Button) findViewById(R.id.button1);
bDel = (Button) findViewById(R.id.button2);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.EditText2);
et3 = (EditText) findViewById(R.id.EditText3);
lv = (ListView) findViewById(R.id.listView1);
al = new ArrayList<String>();
aa = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, al);
lv.setAdapter(aa);
bAdd.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
String str1 = et1.getText().toString();
if (str1.equals("")) {
Toast.makeText(getApplicationContext(),
"Please Enter Item first!!", 0).show();
} else {
al.add(0, str1);
aa.notifyDataSetChanged();
et1.setText("");
}
}
});
bDel.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0 == null) {
Toast.makeText(getApplicationContext(),
"Nothing to delete", 0).show();
} else {
al.remove(0);
aa.notifyDataSetChanged();
}
}
});
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bAdd=(按钮)findViewById(R.id.button1);
bDel=(按钮)findViewById(R.id.button2);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.EditText2);
et3=(EditText)findViewById(R.id.EditText3);
lv=(ListView)findViewById(R.id.listView1);
al=新的ArrayList();
aa=新的ArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1,al);
低压设置适配器(aa);
bAdd.setOnClickListener(新的android.view.view.OnClickListener(){
公共void onClick(视图arg0){
//TODO自动生成的方法存根
字符串str1=et1.getText().toString();
if(str1等于(“”){
Toast.makeText(getApplicationContext(),
“请先输入项!!”,0.show();
}否则{
添加(0,str1);
aa.notifyDataSetChanged();
et1.setText(“”);
}
}
});
bDel.setOnClickListener(新的android.view.view.OnClickListener(){
公共void onClick(视图arg0){
//TODO自动生成的方法存根
如果(arg0==null){
Toast.makeText(getApplicationContext(),
“无需删除”,0.show();
}否则{
al.移除(0);
aa.notifyDataSetChanged();
}
}
});
删除列表之前,只需检查al
列表的大小:
if (arg0 == null || al.isEmpty()) {
Toast.makeText(getApplicationContext(),
"Nothing to delete", 0).show();
} else {
al.remove(0);
aa.notifyDataSetChanged();
}
删除列表之前,只需检查
al
列表的大小:
if (arg0 == null || al.isEmpty()) {
Toast.makeText(getApplicationContext(),
"Nothing to delete", 0).show();
} else {
al.remove(0);
aa.notifyDataSetChanged();
}
在阵列上设置验证,请参见下文
bDel.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0 == null) {
Toast.makeText(getApplicationContext(),
"Nothing to delete", 0).show();
} else {
if(al.size == 0){
Toast.makeText(getApplicationContext(),
"There is no item to delete", 0).show();
return;
}
al.remove(0);
aa.notifyDataSetChanged();
}
}
});
在阵列上设置验证,请参见下文
bDel.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0 == null) {
Toast.makeText(getApplicationContext(),
"Nothing to delete", 0).show();
} else {
if(al.size == 0){
Toast.makeText(getApplicationContext(),
"There is no item to delete", 0).show();
return;
}
al.remove(0);
aa.notifyDataSetChanged();
}
}
});
我还建议在列表中没有任何内容时禁用删除按钮,而不是显示
Toast
。当列表为空时,屏幕上有一个可用的删除按钮会有点混乱。请使用a1.isEmpty()而不是a1.size()==0@Geobits假设我想用新的“项目y”(更新)替换“项目1”从EditText执行此操作的过程是什么?我可能还建议在列表中没有任何内容时禁用删除按钮,而不是显示Toast
。当列表为空时,屏幕上有一个可用的删除按钮有些混乱。请使用a1.isEmpty()而不是a1.size() == 0@Geobits假设如果我想用EditText中的新“Item y”(更新)替换“Item 1”,那么这样做的过程是什么?