0

I have a custom Array List need to save in sqlite ,i converted this array list into string using to string method and saved it in database. Now i need again this Array list.So how can i convert custom Array List form this particular String.

Here's the code i used to convert Array to String

ArrayList<Publication> pb = new Array List<Publication>();
pb.add(new Publication("", ""));
String st = pb.tostring();

Now I added st in SQLite database as a string. My problem is how I revert back to custom ArrayList of publication from this string.

7
  • 1
    post an example of ur string Commented Jan 20, 2017 at 8:52
  • Please share your code Commented Jan 20, 2017 at 8:52
  • How did you saved the Array List? Adding comma (,) after each string? Commented Jan 20, 2017 at 8:52
  • Array List<Publication> pb=new Array List<Publication>(); pb.add(new Publication("","")); String st=pb.tostring(); Now i added "st" in sqlite database as a string.My problem is how i revert back to custom arraylist of publication from this string. Commented Jan 20, 2017 at 8:55
  • @Reema Update your question with code Commented Jan 20, 2017 at 8:55

3 Answers 3

2

You can use Gson Library to convert string to Custom ArrayList. Add this gradle dependency in your app build.gradle

compile 'com.google.code.gson:gson:2.2.+'

then use this code

Gson gson = new Gson();
TypeToken<ArrayList<Publication>> token = new TypeToken<ArrayList<Publication>>() {
    };
ArrayList<Publication> pb = gson.fromJson(str, token.getType());
Sign up to request clarification or add additional context in comments.

5 Comments

I am getting string value from sqlite database like this [common.Publication@8301fc7, common.Publication@11659f4] and your code is not working here.Here common is my package name and Publication is a class name.
What are the parameters of Publication class constructor?
there are multiple fields, like int publication_id; byte[] publication_image; String publication_date; String publication_color
@ReemaSing Don't convert Custom arraylist to string like using toString method. You should use the Gson library to convert custom arraylist to string like String str = new Gson().toJson(pb); And then put this String str into the database.
Thanx.finally its done.whatever i want. Thanx a lot.:)
0

If seperated by ","

String[] strValues = str.split(",");//this is the string u saved in sqlite

/*Use asList method of Arrays class to convert Java String array to ArrayList*/
ArrayList<Publication> aList = new ArrayList<Publication>();
for(i=0; i<strValues.count(); i++)
aList.add(new Publication().setField(strValues[i]));

Now the custom Class Publication

class Publication{
String field;

public void setField(String f) {

field=f;
}

public String getField() {

return field;
}

}

or

for (String value: strValues) {
   aList.add(new Publication(value));
}

class Publication {
   private final String input;

   public Publication(String input) {
      this.input = input;
   }

   // not needed but implemented for completeness    
   public static Publication parse(String input) {
      return new Publication(input);
   }
}



Check this: You wont be able to insert ArrayList directly into Sqlite. Instead, you could use JSONObject (org.json.JSONObject) to insert the ArrayList. Please check below and give it a shot....

//Add values to arraylist item
ArrayList<Publication> item = new ArrayList<Publication>();
item.add  //add the item to array list

To insert,

JSONObject json = new JSONObject();
json.put("uniqueArrays", new JSONArray(items));
String arrayList = json.toString();
Insert the string into db.

To Read, Read the string from db as String,

JSONObject json = new JSONObject(stringreadfromsqlite);
  ArrayList<Publication> items = json.optJSONArray("uniqueArrays");

14 Comments

I am using ArrayList<Publication> ,it means custom object.
Wt are fields in Publication then
@Rheema are there multiple fields in publication
yes,there are multiple fields, like int publication_id; byte[] publication_image; String publication_date; String publication_color;
@ReemaSingh Are u getting wt i'am trying to describe
|
0
  Updates to Mayur Gangurde's Answer for Kotlin:-

Import the gson using this in your app gradle:-

compile 'com.google.code.gson:gson:2.2.+'

Then in your kotlin class Declare custom arraylist object variable

var publicationListing = ArrayList< Publication>()

Then Convert String to custom arraylist like this

val gson = Gson();
val token = object : TypeToken<ArrayList< Publication>>() {}
publicationListing = gson.fromJson(str, token.getType())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.