List log = new ArrayList ();

时间:2020-12-16 04:18:54

I have been trying to use ArrayList.add, when I was putting just .add("some data") it worked fine, when I tried assigning the id - .add(1, "some data"); I keep getting the error when I try running the app - "The application MobileApp(process com.example.mobileapp) has stopped unexpectedly. Please try again.".

我一直在尝试使用ArrayList.add,当我只放了.add(“一些数据”)时它运行正常,当我尝试分配id - .add(1,“some data”);我在尝试运行应用程序时不断收到错误 - “应用程序MobileApp(进程com.example.mobileapp)已意外停止。请再试一次。”

This is the next code that I am running:

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

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

This works fine:

这很好用:

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

What I want to achieve from this:

我想从中实现的目标:

  1. ListView will have list of items, each item would have specific "id".
  2. ListView将包含项目列表,每个项目都有特定的“id”。
  3. On click this "id" will be picked up and user is moved to different layout where further information is shown on item with selected "id".
  4. 点击后,这个“id”将被选中并且用户被移动到不同的布局,其中进一步的信息显示在具有所选“id”的项目上。

2 个解决方案

#1


5  

You are trying to add an item at index 1 which doesn't exist. You will need to change the code to insert into the 0 position first.

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

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

If you need specific IDs that do not correspond to the index, you might want to look into using a Map.

如果您需要与索引不对应的特定ID,则可能需要查看使用Map。

#2


3  

Try this

尝试这个

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

I think you need to start from 0. You cannot insert 1 if we dont have data in 0th position.

我想你需要从0开始。如果我们没有第0个位置的数据,你就不能插入1。

#1


5  

You are trying to add an item at index 1 which doesn't exist. You will need to change the code to insert into the 0 position first.

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

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

If you need specific IDs that do not correspond to the index, you might want to look into using a Map.

如果您需要与索引不对应的特定ID,则可能需要查看使用Map。

#2


3  

Try this

尝试这个

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

I think you need to start from 0. You cannot insert 1 if we dont have data in 0th position.

我想你需要从0开始。如果我们没有第0个位置的数据,你就不能插入1。