0

I have an ArrayList of RowItem where RowItem Class have following variables :

public class RowItem
{
   private Bitmap photo;
   private String photoURL ;
   private int online_status;
   private String name;
   private String last_seen;
   private int delete;
   //Getters and Setters ...
}

I want to save this ArrayList into internal storage. Here is the list of things I have already tried but aren't working :

  1. Implemented exclusion strategy as indicated in here. Excluded Bitmap from saving as I am Lazy Loading Images From URLs to ListView. But I am still getting errors like :

     java.lang.RuntimeException: Unable to pause activity {com.awesome.clique/com.awesome.clique.Home}: java.lang.IllegalArgumentException: class android.text.BoringLayout declares multiple JSON fields named mPaint.
    

    If I am saving in onPause() or onStop() using SharedPreferences(using gson) or Files.

  2. Overridden methods of readObject and writeObject for RowIem class. But it still kept on giving the above error.

Now I am out of options now. I do now think that saving the ArrayList in SQLite database maybe feasible, but I do not know how to save this list into SQLite databse as I have no idea about SQLite and in a glance it seemed very complex. Please help me out.

Community
  • 1
  • 1
SlashGeek
  • 565
  • 5
  • 17

1 Answers1

1

I would suggest you to look at ORMLite, it will simplify how you work with SQLite.

Take a look at how I implemented a simple model here: https://github.com/slidese/BabyFeed/blob/master/src/se/slide/babyfeed/model/FeedLog.java

And have a look (at that same project) how you work with it, it pretty simple.

Your code would look something like this:

@DatabaseTable
public class RowItem
{
   @DatabaseField(generatedId = true)
   private int id;

   @DatabaseField
   private Bitmap photo;

   @DatabaseField
   private String photoURL;

   @DatabaseField
   private int online_status;

   @DatabaseField
   private String name;

   @DatabaseField
   private String last_seen;

   @DatabaseField
   private int delete;

   //Getters and Setters ...
}

And then:

DatabaseManager.getInstance().addRowItem(row);

You create these helper methods, for example a delete could look like this:

public void deleteRowItem(RowItem row) {
    try {
        getHelper().getRowDao().delete(row);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Mike
  • 1,000
  • 1
  • 10
  • 18
  • Can you tell me , how will I delete a particular row ? – SlashGeek Dec 08 '13 at 09:01
  • It's a bit too much code to paste here. But you could look at how I'm working with it in the DatabaseManager and DatabaseHelper classes here: https://github.com/slidese/BabyFeed/blob/master/src/se/slide/babyfeed/db/DatabaseManager.java – Mike Dec 08 '13 at 09:29
  • In your code there is a function for adding FeedLogs, its okay but I am a little puzzled that how will I delete a particular FeedLog or RowItem ? If you could just paste the function for delete only, that would be very helpful. – SlashGeek Dec 08 '13 at 09:51
  • 1
    @SlashGeek I added a delete helper method to the answer. – Mike Dec 08 '13 at 10:50
  • Hey I am getting a NullPointerException along with the following warnings : ": unable to find class referenced in signature (Lcom/j256/ormlite/dao/Dao;) VFY: unable to resolve interface method: Lcom/j256/ormlite/dao/Dao;.create (Ljava/lang/Object;)I". What is happening ? – SlashGeek Dec 08 '13 at 11:38
  • Have you added the ORMLite jar files to your project? – Mike Dec 08 '13 at 11:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42739/discussion-between-slashgeek-and-mike) – SlashGeek Dec 08 '13 at 11:44
  • Sorry, I need to leave in a few minutes. Please have a look at the ORMLite page for the jar files you need and look how I implemented it in my app. – Mike Dec 08 '13 at 11:46
  • This line : listItems = (ArrayList)DatabaseManager.getInstance().getAllRowItems(); is throwing NullPointerException , and I can't find out why. Although the warnings are not showing up as I have also added ORMLite-core-4.47.jar to the project. – SlashGeek Dec 08 '13 at 12:11
  • Hey I got the bug,the getInstance() was the convict. It was just returning the instance without checking it was null or not. Thanks anyways :) – SlashGeek Dec 08 '13 at 13:44