【问题标题】:Android - Find the index of ListView item by textAndroid - 通过文本查找 ListView 项的索引
【发布时间】:2021-05-31 20:13:04
【问题描述】:

我有两个字符串列表。一个包含 ListView 的项目,另一个包含第一个列表中的一些项目。

让我告诉你: 这基本上就是我的 ListView,我们称之为Ore

<string-array name="ore">
        <item>10:00 - 11:00</item>
        <item>11:00 - 12:00</item>
        <item>12:00 - 13:00</item>
        <item>13:00 - 14:00</item>
        <item>14:00 - 15:00</item>
        <item>15:00 - 16:00</item>
        <item>16:00 - 17:00</item>
        <item>17:00 - 18:00</item>
        <item>18:00 - 19:00</item>
        <item>19:00 - 20:00</item>
        <item>20:00 - 21:00</item>
        <item>21:00 - 22:00</item>
    </string-array>

这是我的第一个列表(它来自我的strings.xml,但我已设法将其转换为字符串列表)。

第二个列表,我们称之为CheckOre

12:00 - 13:00, 19:00 - 20:00, 15:00 - 16:00
for(String ora : CheckOre){
      for(String stringOra: Ore){
           if(ora.equals(stringOra)){
               // i want to get the index of ListView item by the text from Ore
          }
      }
}

正如if 条件中所说,我想通过文本获取 ListView 项的索引。 比如ora14:00 - 15:00,我想得到索引,也就是4

有什么方法可以帮助我做到这一点吗?谢谢!

【问题讨论】:

  • 如果您要使用没有索引的 for 循环,那么显然您将无法使用它。所以创建你自己的计数器变量,你可以增加,或者你可以使用带有索引的 for 循环,甚至可以找到一种方法来直接在列表中找到项目的索引。

标签: java android


【解决方案1】:

此方法将返回整数 ArrayList 中所有相同的索引:

public ArrayList<Integer> getSameIndexes(ArrayList<String> arr1, ArrayList<String> arr2) {
     ArrayList<Integer> indexes = new ArrayList<Integer>();
     for(int x = 0; x < arr1.size(); x++) {
        for(int y = 0; y < arr2.size(); y++) {
           if(arr1.get(x).equals(arr2.get(y)))
               indexes.add(y);
         }
     }
     return indexes;
}

只需使用getSameIndexes(CheckOre, Ore); 调用它

您也可以使用:

public int getIndex(ArrayList<String> arr1, ArrayList<String> arr2) {
     for(int x = 0; x < arr1.size(); x++) {
          for (int y = 0; y < arr2.size(); y++) {
               if (arr1.get(x).equals(arr2.get(y)))
                    return y;
          }
     }
     return -1;
}

在调用getIndex(CheckOre, Ore);时会返回匹配项的第一个元素

【讨论】:

  • 我不知道谁对你投了反对票,但这应该可以。它解决了我的问题,我非常感谢你!!!我尝试过使用 switch,但它有很多代码,你的解决方案是完美的!谢谢!!!
  • 使用嵌套的 for 循环? indexOf() 是更好的选择吗?@Marius-Marian Burlacu
【解决方案2】:

您可以在 getView() 中使用标签,如下所示,

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      String test = getItem(position);
      convertView.setTag(test);
  }

我在 RecyclerView 中尝试过而不是在 ListView 中,但它应该可以工作。另外我会说最好的方法是a_local_nobody 所说的。

【讨论】:

    【解决方案3】:

    使用 indexOf 方法

    Kotlin 语言

    fun main() {
        val listOre = listOf(
            "10:00 - 11:00",
            "11:00 - 12:00",
            "12:00 - 13:00",
            "13:00 - 14:00",
            "14:00 - 15:00",
            "15:00 - 16:00",
            "16:00 - 17:00",
            "17:00 - 18:00",
            "18:00 - 19:00",
            "19:00 - 20:00",
            "20:00 - 21:00",
            "21:00 - 22:00",
        )
    
        val checkOre = listOf(
            "12:00 - 13:00", "19:00 - 20:00", "15:00 - 16:00"
        )
    
        for (i in checkOre) {
            println(listOre.indexOf(i))
        }
    }
    

    结果:

    2
    9
    5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      相关资源
      最近更新 更多