简体   繁体   English

删除空arraylist中的元素会使我的应用程序崩溃

[英]Deleting elements in empty arraylist crashes my app

I have an arraylist for my spinner, 1 add button to add element into the list and 1 delete button to delete the element inside the list. 我有一个用于旋转器的arraylist,1个添加按钮可将元素添加到列表中,以及1个删除按钮可删除列表中的元素。 The elements that I added into the list will show in the spinner. 我添加到列表中的元素将显示在微调器中。 Initially the arraylist is empty with nothing inside. 最初,arraylist是空的,里面什么也没有。 When it is empty and I press the delete button, means that I am trying to delete elements in a arraylist with no element inside and this makes my app crashes. 当它是空的并且我按下删除按钮时,意味着我试图删除一个数组列表中没有元素的元素,这会使我的应用程序崩溃。

So, I wanted to add a toast to replace the delete function when the list is empty. 因此,我想添加一个祝酒词,以在列表为空时替换删除功能。 When the list is not empty, then the delete function will come back. 当列表不为空时,删除功能将返回。

Any solution for this? 有什么解决办法吗?

    spinner = (Spinner) findViewById(R.id.spinner1);
    adp = new ArrayAdapter<String>(CarSelection.this,android.R.layout.simple_spinner_item, list);
    adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adp);
    spinner.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                        AdapterView<?> parent, View view, final int position, long id) {
                            Button delete = (Button) findViewById(R.id.delete);

                            View.OnClickListener del = new View.OnClickListener() {

                                @Override
                                public void onClick(View view) {
                                    list.remove(position);
                                }

Lets say, your ArrayList is called mList , your delete function should look something like - 可以说,您的ArrayList称为mList ,您的delete函数应类似于-

public void deleteElement(int pos) {

    if(mList.isEmpty()) {
        //Toast
        Toast.makeText(yourContext, "Ooi, list is empty", Toast.LENGTH_SHORT).show();
    } else {
        mList.remove(pos);
    }
}

UPDATE 更新

@Override                                
public void onClick(View view) {

    if(list.isEmpty()) {
        //Your Toast
        Toast.makeText(yourContext, "Ooi, list is empty", Toast.LENGTH_SHORT).show();
    } else {
        list.remove(position);
    }
}

Firstly,IMHO for better UX, you should not display the spinner if the list is empty .You can show toast message to the user saying that you cant perform this operation. 首先,IMHO为获得更好的用户体验, 如果列表为空则不应该显示微调框。您可以向用户显示吐司消息,表明您无法执行此操作。

Anyways here is the code snippet you can use to do the check.You can put this check in whichever place you want 无论如何,这里是您可以用来进行检查的代码段。您可以将其放置在所需的任何位置

 if(!list.isEmpty())
      //list is empty
    else
     list is not empty

You should test for the "empty" case. 您应该测试“空”情况。 Supposing an array named "elements": 假设一个名为“ elements”的数组:

if elements.isEmpty() {
  deleteButton.disable();
}

The best is to disable the delete button when there is no elements in the array. 最好是在数组中没有元素时禁用删除按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM