【问题标题】:List<String> log = new ArrayList<String>();List<String> log = new ArrayList<String>();
【发布时间】:2013-03-27 02:56:12
【问题描述】:

我一直在尝试使用ArrayList.add,当我只输入.add("some data") 时它工作正常,当我尝试分配 id 时 - .add(1, "some data"); 我在尝试运行应用程序时不断收到错误 - “应用程序MobileApp(process com.example.mobileapp) 已意外停止。请重试。”。

这是我正在运行的下一个代码:

    List<String> log = new ArrayList<String>();
    log.add(1, "some data");

这很好用:

    List<String> log = new ArrayList<String>();
    log.add("some data");

我想由此实现的目标:

  1. ListView 将有项目列表,每个项目都有特定的“id”。
  2. 点击后,该“id”将被拾取,用户将被移动到不同的布局,其中在带有选定“id”的项目上显示更多信息。

【问题讨论】:

  • 你能粘贴你的 LogCat 输出吗?
  • from doc Throws: IndexOutOfBoundsException - if the index is out of range (index &lt; 0 || index &gt; size()) size() is zero your index is 1 ...
  • 如果你想要each item would have specific "id",你应该考虑Map&lt;Integer, String&gt;
  • 请学习如何查看logcat的输出。你会看到有一个 ArrayIndexOutOfBoundsException 被抛出。

标签: android listview arraylist


【解决方案1】:

您正在尝试在索引 1 处添加一个不存在的项目。您需要先更改代码以插入 0 位置。

List<String> log = new ArrayList<String>();
log.add(0, "some data");

如果您需要与索引不对应的特定 ID,您可能需要考虑使用 Map

【讨论】:

【解决方案2】:

试试这个

List<String> log = new ArrayList<String>();
log.add(0, "some data");//Changed from 1 to 0

我认为您需要从 0 开始。如果我们在第 0 位没有数据,则无法插入 1。

【讨论】:

  • 我想你领先我一分钟 :)
猜你喜欢
  • 2014-07-22
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 2015-12-07
  • 2014-01-09
  • 1970-01-01
相关资源
最近更新 更多