J1and1
J1and1

Reputation: 920

How to use List<Data> in android?

How should I use List<Data> dat = new List<Data>(); to temporary store data in my software? "Data" is a class in my code with variables(mainly Strings). When I try that method it doesn't store data.

Upvotes: 2

Views: 12455

Answers (2)

MAC
MAC

Reputation: 15847

List<Data> lsData = new ArrayList<Data>();

for(int i=0;i<5;i++)
{
    Data d = new Data();
    d.fname="fname";
    d.lname="lname";
    lsData.add(d);
}

Your Data class (Always make a Bean class to manage data)

public class Data
{
  public Data()
  {
  }
  public String fname,lname;
}

you can also get your data of particular position

String fname = lsData.get(2).fname;
String lname = lsData.get(2).lname;

Upvotes: 3

assylias
assylias

Reputation: 328669

List is an interface, so you can't instantiate it (call new List()) as it does not have a concrete implementation. To keep it simple, use one of the existing implementations, for example:

List<Data> dat = new ArrayList<Data>();

You can then use it like this:

Data data = new Data();
//initialise data here
dat.add(data);

You would probably benefit from reading the Java Tutorial on Collections.

Upvotes: 11

Related Questions