mardi 13 janvier 2015

how to insert into model in ActiveAndroid?

I know how to insert string into model in ActiveAndroid. like this.



Book book = new Book();
book.title = "Book";
book.save();


I could understood the way insert "String" or "int" only. then, how can I insert into model?


And when do I have to describe "save"? I think describe in Activity. However,in this code, describe in Model and Activity.


Please tell me how to resolve this problem.



public static void jsonParse() {

new Delete().from(Book.class).execute();
ActiveAndroid.beginTransaction();

try {
AndroidTestActivity.mContext = app.getContext();

AssetManager assetManager = mContext.getAssets();

InputStream inputStream = assetManager.open(FILE_NAMES);

JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream));
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray jArray = parser.parse(jsonReader).getAsJsonArray();

Book book = new Book();
/** how can I insert into model? */
book.title =
book.author =
book.publisher =
book.releaseDate =

book.save();

} catch (IOException e) {
e.printStackTrace();
}
finally {
ActiveAndroid.setTransactionSuccessful();
}
ActiveAndroid.endTransaction();


}





@Table(name = "Book")
public class Book extends Model {
private static final String TAG = Book.class.getSimpleName();

@Column(name = "title")
public String title;

@Column(name = "author")
public String author;

@Column(name = "publisher")
public String publisher;

@Column(name = "release_date")
public String releaseDate;

public Book() {
super();
}
public Book(String title, String author, String publisher, String date) {
super();
this.title = title;
this.author = author;
this.publisher = publisher;
this.releaseDate = date;
}

public static Book fromJson(JSONObject jsonObject){
Book b = new Book();

try{
b.title = jsonObject.getString("title");
b.author = jsonObject.getString("author");
b.publisher = jsonObject.getString("publisher");
b.releaseDate = jsonObject.getString("release_date");
b.save();
}catch (JSONException e) {
e.printStackTrace();
return null;
}
//return new object
return b;
}
}

Aucun commentaire:

Enregistrer un commentaire