Saving data in android (using processing.core)
I am using processing.core.*
for some simple graphics app for android.
This is what my code usually looks like:
package com.example.ball;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.EditText;
import processing.core.*;
public class MainActivity extends PApplet {
String qwe;
public void setup()
{
size(displayHeight,displayWidth);
}
int x=50;
int y=50;
int temp=1;
int flag;
public void draw()
{
sub te = new sub(this) ;
background(0);
// text("hello",displayHeight/2,displayWidth/2);
x+=temp;
//text("hello",150,150);
te.prin();
if(x+41>400)
{
temp=-1;
}
else if(x-40<0)
temp=1;
ellipse(50,50,100,100);
smooth();
if(mousePressed)
{
int x=mouseX;
int y=mouseY;
float a=pow((x-50),2);
float b=pow((y-50),2);
double d=Math.pow(a+b,.5);
if(d<50)
{text("hellossaww",150,150);
//currentValue=5;
}
}
}
}
Now, I want to be able to save user input data into internal storage (I tried reading the 'internal storage','external storage' etc, but I couldn't understand anything).
I want to be able to save data value like int, and the objects I make, and then call the values.
(PS this is the only class I have)
Please, I really need help in this, but I am new to it.
You can store data in a number of ways , like shared preferences,SQLlite etc.
To store user data using shared preferences you can actually use the following code.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Key","Value");
editor.commit();
To retrieve values from shared preferences:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("key","");
if(!name.equalsIgnoreCase(""))
{
/* Edit the value here of key as you might find it suitable*/
}
These values are stored in an xml file in the file system, and if you want you can manually create an xml file too for storing values.
as you mentioned you are new to android development I recommend you to visit this harward site and review the lectures. http://cs76.tv/2012/spring/
lecture 6 is about storage so you might wanna have a look.
链接地址: http://www.djcxy.com/p/90014.html