0

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

Below is how I am initialising the ArrayList

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

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

passwords.add(new SearchData()
{

});

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.

What am I doing wrong?

1
  • Are you asking how to statically initialise passwords? Commented Dec 2, 2012 at 14:58

4 Answers 4

3

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);
Sign up to request clarification or add additional context in comments.

Comments

3

Create a constructor for your class.

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;

   SearchData(int id, String category, String company......){

         this.id = id;
         this.category = category;
         this.company = company;
         ...
   }
}

Then use it like this:

passwords.add(new SearchData(0,"Category1", "Company1"......));

Comments

0

Create an object and store reference to it:

SeachData searchData = new SearchData();

Set the properties you want to set:

searchData.setId(123);

...so on

searchData. Ctrl+Space should show the intellisense now..

Adding the search reference to the list:

list.add(searchData);

Comments

0
SearchData sd = new SearchData();
sd.id = 0;
sd.category = "hello";
passwords.add(sd);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.