Subject: You've tried the rest, try the best

From: Daily OTC Leaders <[email-masked]>
Date: Wed, 10 Dec 2014 11:22:09 -0800

When is the last time you had a really good pick drop in your lap?  

It's time to seek quality, and not quantity.  Don't get the idea though that you have to 
wait a long time for a good pick.  We have daily picks that experience terrific volume
and meteoric performance.

http://www.youreportandwatchservices.com/gustiest/mooning/azure/fatalistically/producers.html

We're not just another Penny Subscription Service.  Simply put...

We're the best.

To End Communication From USA-SR Bradford:
137 Franklin, Hamden, CT, 06517

~Jasp-- ID & MEM # 18HD742F

1
down vote
The right way is to convert to a String, and then put it in SharedPreferences. You can convert the List to a String like this:
public static String listIntToString(List<Integer> list) {
    StringBuilder s = new StringBuilder();
    for (int i: list)
        s.append(i).append(" ");
    return s.toString();
}
Now to get back to a List<Integer>:
public static List<Integer> stringToListInt(String s) {
    List<Integer> result = new ArrayList<>();
    Scanner scan = new Scanner(s);
    while (scan.hasNextInt())
        result.add(scan.nextInt());
    scan.close();
    return result;
}
This is much cleaner than messing about with files or databases or XML for something so simple.

Actually if you want to get cute then you can just convert the List<Integer> to a String using its .toString() method, and then use this to get back to the List<Integer>:
public static List<Integer> defaultStringToListInt(String s) {
    List<Integer> result = new ArrayList<>();
    Scanner scan = new Scanner(s);
    scan.useDelimiter(Pattern.compile("[, \\[\\]]+"));
    while (scan.hasNextInt())
        result.add(scan.nextInt());
    scan.close();
    return result;
}
That way, you only have to define one helper method. This works because the .toString() method on a List<Integer> comes out as something like
[1, 22, -3, 4, 5]
and the code above scans through looking for integers, using square brackets, commas and spaces as delimiters.
share|improve this answer
edited 24 mins ago

answered 1 hour ago

chiastic-security
10.2k2734

I tried this, but it throws a NullPointerException "attempt to get length of null array", so I am doing something wrong, but what?  Nifty 46 mins ago
1

@Nifty on which line? The code I gave you doesn't use any arrays...  chiastic-security 32 mins ago
add a comment

up vote
0
down vote
You could either use a database to store information that your app will retrieve when it restarts or (probably the easier method) is to save your list of integers to a file and read from it when your app restarts.
share|improve this answer
answered 1 hour ago

RobPio
685

So what method is preferred? I find it hard to make a choice because I'm not familiar with one of them.  Nifty1 hour ago

I'd use the save to file method.  RobPio 1 hour ago

Actually I take that back. Read up about SQLite. that might be the better way  RobPio 1 hour ago
add a comment
up vote
0
down vote
I would suggest creating an XML, and then reading it to gather your data each time your app starts. You could do something like the following to write your XML;
// Create your document and it's root element.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element root = doc.createElement("root");
doc.appendChild(root);
// Loop through your list and create an xml element for each list element.
for(Integer i : yourList){
     Element listItem = doc.createElement("integer");       
     listItem.setAttribute("value", i.toString());
     root.appendChild(listItem);
}
// write the the Document to where-ever you want.
FileOutputStream fos = yourContext.openFileOutput("data_store.xml", Context.MODE_PRIVATE);
TransformerFactory factory = new TransformerFactory();
Transformer transformer = factory.newTransformer();
DOMSource source = new DOMSource(root);
StreamResult result = new StreamResult(fos);
transformer(source, result);
And to read your XML simply;
FileInputStream fis = yourContext.openFileInput("data_store.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(fis);
// Get & loop through a list of all integer elements
NodeList nodes = doc.getElementsByTagName("integer");
for(Node n : nodes){
    if(n.getNodeType() == Node.ELEMENT_NODE){
        Element e = (Element) n;
        yourList.add(Integer.parseInt(e.getAttribute("value")));
    }        
}