Skip to content Skip to sidebar Skip to footer

Adding Data To An Array List

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 My

Solution 1:

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

Solution 2:

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

Solution 3:

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

Solution 4:

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

Post a Comment for "Adding Data To An Array List"