将数据添加到数组列表 [英] Adding data to an array list

查看:74
本文介绍了将数据添加到数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Android 项目,我正在尝试使用 ArrayList 类型的 MyClass.我试图将 ArrayList 中的数据存储到 MyClass 中的每个变量.下面是我正在使用的代码.

I'm working on an Android project and I'm trying to make use of an ArrayList which is of type MyClass. I am trying to store data in the ArrayList to each of the variables within MyClass. Below is the code I am using.

下面是将使用的变量的类.

Below is the Class with the variables that will be used.

class SearchData
{
    public int id;
    public String category;
    public String company;
    public String loginAction;
    public String username;
    public String password;
    public String type;
    public String appName;
}

下面是我如何初始化 ArrayList

ArrayList<SearchData> passwords = new ArrayList<SearchData>();

下面是我如何尝试将新数据添加到 ArrayList

And below is how I am trying to add new data to the ArrayList

passwords.add(new SearchData()
{

});

我不知道如何使用我需要将变量设置为的数据从类中设置变量.在 C# 中,我比 Java 更了解,我可以执行以下操作:

I can't figure out how to then set the variables from within the class with the data that I need them to be set to. In C#, which I know more about than Java, I can do the following:

passwords.add(new SearchData()
{
    id = 0,
    category = "hello"                      
});

但是,我没有看到 Intellisense 帮助中显示的类中的任何变量.

However, I'm not seeing any of the variables that are within the class being shown in the Intellisense help.

我做错了什么?

推荐答案

需要先创建一个对象并设置好所有的属性,然后添加到List:-

You need to create an object and set all the attributes first, and then add it to the List: -

SearchData searchData = new SearchData();
searchData.setId(1);
searchData.setCategory(category);
...

passwords.add(searchData);

这篇关于将数据添加到数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆