【问题标题】:Android: Index Out Of Bound Exception in ArrayListAndroid:ArrayList 中的索引越界异常
【发布时间】:2012-10-19 13:45:03
【问题描述】:

我正在使用数组适配器在列表视图中显示数组列表。我可以添加和删除项。假设如果列表视图中没有 item,如果我选择 delete,则它显示的 Index out of Bind Exception。我需要的是它应该显示像“没有要删除的项目”这样的吐司。澄清我的专家!我的代码如下:

@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();
            }
        }
    });

【问题讨论】:

    标签: android indexoutofboundsexception


    【解决方案1】:

    删除之前只需检查al 列表的大小:

            if (arg0 == null || al.isEmpty()) {
                Toast.makeText(getApplicationContext(),
                        "Nothing to delete", 0).show();
            } else {
                al.remove(0);
                aa.notifyDataSetChanged();
            }
    

    【讨论】:

    • 我可能还建议在列表中没有任何内容时禁用“删除”按钮,而不是显示Toast。当列表为空时,屏幕上有一个可用的删除按钮有点令人困惑。
    • 使用 a1.isEmpty() 代替 a1.size() == 0
    • @Geobits 假设如果我想用 EditText 中的新 'Item y'(Update) 替换 'Item 1' 这样做的程序是什么?
    【解决方案2】:

    在数组上设置验证,见下文

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-19
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多