Input on Persisting a DataStructure - android

I have an android application that reads an XML file and presents some information to the user. The data structure is a simple ArrayList. The user goes through the list and they may make changes or they may not make changes just depending. When they are done they save.
Eventually we want that data on the RDBMS but they may not have a data connection at that exact moment so I need to persist it. Even if they do have a data connection I would probably feel comfortable knowing I have the data structure serialized to the sd card.
So step one is do I persist it in a binary state (it is a very small data object) or serialize it out to XML which is what I was thinking at first. That way it can be uploaded to a web service in it's xml form.
If I serialize it out to XML are there any libraries / example I can read up on. I use SAX to parse it but was thinking there must be something like a simple overload to dump it out????
pseudo code:
ArrayList<MyOb>;
MyObj.Serialize("doc.xml");
If I persist it in it's binary state what are the steps I need to consider when the user does have a data connection and it is time ti upload.
TIA
JB

You can use a great library JAXB(Java Architecture for XML Binding) for
JAVA Beans <---> XML Data
JAXB Basic Tutorial
Here is some sample code to give you some taste of it
#XmlRootElement
public class Customer {
String name;
int age;
int id;
public String getName() {
return name;
}
#XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
#XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
#XmlAttribute
public void setId(int id) {
this.id = id;
}
}
XML formed using this code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
<age>29</age>
<name>mkyong</name>
</customer>

I'm going with persisting the data as xml on the sdcard. It's the best method from what I can discern.

Related

How can I remove "m" prefix from greenDAO's generated getters and setters?

GreenDAO is searching for getters and setters with the "m" prefix included in the name in its generated classes.
For example, I've set the entity to not generate any getters and setters but it still looks for, let's say, getMDate() for my private Date mDate, instead of my manually created getDate() method. Same goes for setters.
This is just breaking my adherence to the Android Code Style Guidelines.
Is there any way to configure greenDAO to use the code style standards that I've set out in Android Studio when it generates code?
EDIT: Just to make things clearer, I've added some code to get my point across. I wrote the above question half knocked out on antihistamines due to having chronic hayfever while I was trying to work and just wanted to get to sleep so I apologise if it wasn't enough to work with.
#Entity(generateConstructors = false, generateGettersSetters = false)
public class Day {
#Id
private long mId;
#Unique
#NotNull
private Date mDate;
#ToMany(referencedJoinProperty = "mDayId")
private List<Goal> mGoals;
public Day(long id) {
this.mId = id;
mGoals = new ArrayList<Goal>();
}
public Day(long id, Date date) {
this.mId = id;
this.mDate = date;
}
public long getId() {
return mId;
}
public void setId(long id) {
mId = id;
}
public Date getDate() {
return mDate;
}
public void setDate(Date date) {
mDate = date;
}
public List<Goal> getGoals() {
return mGoals;
}
public void setGoals(List<Goal> goals) {
mGoals = goals;
}
}
Above is my Day class. As you can see I've disabled generation of the getters and setters in the #Entity annotation, and put my own in there. I've set up Android Studio to take the "m" into consideration when I use alt+enter to create getters and setters for each of my fields.
public Date getMDate() {
return mDate;
}
public void setMDate(Date mDate) {
this.mDate = mDate;
}
Here is an example of the getters and setters that greenDAO generates for my code given the field private Date mDate. This is breaking the code style guidelines in that the local variables include the "m" prefix and also that the method names include it as well (maybe a bit of a nit-pick there, but you can set up Android Studio so it doesn't do that which makes me think it shouldn't be like that).
With my getters and setters there, greenDAO still thinks the getters and setters are missing, which results in them being added twice. Once as the ones I put in, and another as what you see above. This also results in the generation of the code below.
#Override
protected final void bindValues(DatabaseStatement stmt, Day entity) {
stmt.clearBindings();
stmt.bindLong(1, entity.getMId());
stmt.bindLong(2, entity.getMDate().getTime());
}
#Override
protected final void bindValues(SQLiteStatement stmt, Day entity) {
stmt.clearBindings();
stmt.bindLong(1, entity.getMId());
stmt.bindLong(2, entity.getMDate().getTime());
}
Here is some of the code from the class DayDao which is generated from greenDAO. It's still using the names for the getters (and also setters) that it would have used to generate its own getters and setters (getMDate() instead of getDate()) if I didn't disable their generation on the Day entity class. I can't change this code because it just switches back the next time I build the project, and there is my problem.
My question is: how can I get greenDAO to take the "m" prefix thing into consideration when it generates its code and get it to use the getters and setters that I have set out myself? Or even get it to generate getters and setters itself without the "m" being included in the name and local variables?
The solution that I decided to use for this ended up being rather disappointing.
It involved just making my properties all public instead of private. Still conforming to the code style, these properties would no longer use the m prefix ... or any prefix, for that matter. It's not a very satisfactory solution as now my data can be accessed and set directly, just for a simple naming convention to be conformed to.
I (personally) disagree with people saying that using naming conventions hurts readability. If you structure your code well enough and give enough of a description in Javadoc comments and the like, you'll know exactly what something is doing. Having said that, if using the conventions is going to create such code as in my question above, then I guess that this is a situation where it does, and a reason not to use the code style in my other projects and still stick to the access that I want for my data.

How to open a Intent with data according to the item clicked on a ListView?

I have a ListView, actually generated by parsing a XML file downloaded from Internet.
This XML file contains data about a person: IdNumber, Name, Age, PhotoURL, Birthday, Phone numbers, Email account, etc.
I get all the XML data when generating the ListView, but on each row I show some values of the person (not all), just name, age, photo (from the PhotoURL) and email.
I would like to get the "IdNumber" to parse it to the Activity that shows all the info, this activity should read the "IdNumber", get all the data of only that person and show it.
How can I parse a value that I'm not using on my ListView?
Thanks in advance,
Herni
For each row in the listView create an Object with all the fields you've originally parsed.
You will create a model that like this:
public class Person
{
String idNumber,Name,Email;
public void SetIdNumber(String p_value)
{
this.idNumber = p_value;
}
public void SetName(String p_value)
{
this.Name= p_value;
}
public void SetEmail(String p_value)
{
this.Email= p_value;
}
//Get Methods
public String GetIdNumber()
{
return idNumber;
}
public String GetName()
{
return Name;
}
public String GetEmail()
{
return Email;
}
When you parse your xml you will create an ArrayList list; and fill after the 'list' you will use this everywhere you want.
For example when you click your first item of your listview you could ;
Person p = list.get(position that clicked your listview);
Now you will use your Person model. This is simple object oriant man.

How to parse a regular String to an XML file

What I'm trying to do is to parse an object into a String, and then , parse it into an XML so any other language can translate it.
Figure out this object:
public class DatosPac
{
private String nombre;
private String apellidos;
private String dni;
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellidos() {
return apellidos;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
public String getDni() {
return dni;
}
public void setDni(String dni) {
this.dni = dni;
}
}
What I want to do is, parse it into a common XML between Android and .Net so both languages can translate the same object. The way to communicate both languages will be using Web Services, so the Web Service will receive a String, transalte it into the object and then use the information. Bidirectionally. I mean, Android will be able to receive an object parsed from .Net, and .Net will be able to receive the same object from Android. To be able to do this, I think I need to convert them into the same XML, but I don't know how to do it in Android.
Thanks in advance.
There are several XML serializing and de-serializing libraries available for Android. And I am sure the same's the case with .NET.
You set up your objects as POJOs and with a few annotations, you can serialize/deserialize in a few lines of code. In the Android world, I personally prefer Simple, but there are various other libraries available.
A more compact, (and more efficient, in terms of parsing) data representation format is JSON. There are multiple libraries available for parsing and constructing JSON too. My preferred one for Android is Gson.
EDIT: I believe I was a bit too quick! I didn't notice the android tag and assumed a .net context. Still, one bit stands: You probably want to serialize, not to "parse" the object, into XML.

How to fill a spinner with content?

guys I'm very new to the Java word, but i share part of the knowledge because of my c# background, anyways i started developing for android and I'm running into a few snags like the following.
I usually program very OOP so i made all my objects and now i got a very common public class User with things like:
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
and many other properties in it. Also i have a class UserHelper
where i have a populated array with all the users been pulled by the queries in the Helper
public ArrayList< User > getCurrentUsers() { return currentUsers;
}
well... the thing is, i want to be able to populate a spinner with a the value returned from getFirstName as the Display and getId obviously as the Id. I know exactly how to do this in C# but i been trying to fight with Cursors and doing some reading around but nothing, so i figured that it would be an interesting question.
ANYONE CAN SHOW ME HOW TO DO IT PLEASE?
Hi Alex look this examples
http://developer.android.com/resources/tutorials/views/hello-spinner.html
http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views

How can i improve my app

I have an app which present some beaches to the users.there is a list view with the name of every beach and when the user press on a name it opens a new activity with a photo and some text.i have created a .java class for every beach( same copy-paste code ) and a common .xml file. is there any better way to do it?for example to have all the beaches and their text in a db?
why don't you just instance the same class but with different parameters in the constructor?
Something like this:
public class Beach{
protected String name;
protected String pathImage;
public Beach(String name, String pathImage){
this.name = name;
this.pathImage = pathImage;
}
}
//Somewhere else in your application...
Beach beach1 = new Beach("Cancun","/images/cancun.png");
Beach beach2 = new Beach("Miami","/images/miami.png");
I would store the beach information in a SQLlite database, then just create a single Beach view that knows how to display the information from the database. A problem with this is that you might want to build a simple tool to allow you to manage the information in the database so you don't have to do it through queries on the command line.
You could ofcourse create a (abstract) superclass Beach.java and let other beaches extend that class. That way, you've got less redundant code.
public abstract class Beach{
protected String name;
public Beach(String name){
this.name = name;
}
public abstract String getOtherInfo();
}
public class FirstBeach extends Beach{
public FirstBeach(){
super("FirstBeach");
}
public String getOtherInfo(){
return "someInfo";
}
}

Categories

Resources