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

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

问题描述

我工作的一个Android项目,我试图让使用的ArrayList 这类型的 MyClass的。我试图在 MyClass的的ArrayList 来每个变量的数据存储。下面是code我使用。

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"                      
});

不过,我没有看到任何是类内的变量被显示在智能感知帮助。

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

我在做什么错了?

推荐答案

您需要创建一个对象,第一次设置的所有属性,然后将其添加到列表 -

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天全站免登陆