【问题标题】:how to remove a string from arraylist of strings, ANDROID如何从字符串数组列表中删除字符串,ANDROID
【发布时间】:2013-10-17 18:01:42
【问题描述】:

我创建了一个字符串数组列表

List<String> textArray = new ArrayList<String>();

然后我将字符串(我从edittext获得)添加到textArray,如下所示

String text = editText1.getText().toString();
textArray.add(text);

现在我创建了一个按钮,并且需要在单击按钮时从数组中删除字符串。但我不知道该怎么做。 我知道对于位图数组,我们使用回收从数组中清除位图,但请建议我如何从数组列表中删除或清除字符串。

【问题讨论】:

    标签: android string arraylist android-edittext


    【解决方案1】:

    您可以调用以下之一:

    删除所有条目

    textArray.clear();
    

    从特定位置移除(例如第一项)

    textArray.remove(0);
    

    删除特定字符串(equals 你的)

    textArray.remove("myString");
    

    【讨论】:

    • textArray.remove("myString");不适用于自定义对象!
    • 最初的问题是关于字符串的。所以是的,它不适用于自定义对象。原因是您的自定义对象不“相等”。这更多的是语言问题。检查tutorials.jenkov.com/java-collections/hashcode-equals.html
    【解决方案2】:

    您可以根据需要使用不止一种方法。

    textArray.clear(); 
    

    它将清除整个 ArrayList。

    如果您只想从索引中删除一些特定数据,那么,

    textArray.remove(INDEX TO REMOVE);
    

    【讨论】:

      【解决方案3】:

      试试这个..

      从 textArray 数组列表中获取位置

      int pos = textArray.indexOf(text);
      

      然后从字符串位置移除

          textArray.remove(pos);
      

      因为我们不能直接删除字符串。我们可以删除包含位置的字符串。

      【讨论】:

        【解决方案4】:

        试试这个

        String text = editText1.getText().toString();
        textArray.add(text);
        

        删除

        textArray.remove(text);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-12
          • 2021-06-24
          • 2011-04-20
          • 1970-01-01
          • 2015-12-01
          • 2015-04-23
          相关资源
          最近更新 更多