-1

I was making an app and all of sudden I got error that code is too long :(

I didn't know about this restriction in Java and I have already added a lot of arrays.
So I can't go back and create a database.

If anyone knows how to convert this:

list = new ArrayList<DataObject>();
list.add(new DataObject("Aback", "پُٺ","Toward the back or rear; backward"));
list.add(new DataObject("Abacus", "انگ","A board, tray, or table, divided into perforated compartments, for holding cups, bottles, or the like; a kind of cupboard, buffet, or sideboard."));

into a String and call that string in that ArrayList?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Murtaza
  • 61
  • 8
  • Its not clear what you are asking. Please provide more detail/example on your desired output – IAmGroot Oct 12 '17 at 11:57
  • I want to convert that ArrayList in String.xml so that i can define all my ArrayList inside a String.xml and call that String.xml in that ArrayList for avoiding that restriction of Long Code in java @Doomsknight – Murtaza Oct 12 '17 at 12:00
  • https://stackoverflow.com/a/31285038/940834 – IAmGroot Oct 12 '17 at 13:32

3 Answers3

0

just to give a vary basic idea , iterate through your list, get the string from your Object save it to xml. for(DataObject dobject: objectList){ String someString = dobject.getYourString(); save(someString); }

himel
  • 500
  • 5
  • 14
  • I didn't understand it. can you please be more specific ? – Murtaza Oct 12 '17 at 12:13
  • if you can iterate through your arraylist of DataObject , then you can get the String you want to save from the DataObject and save it. also how do you add String.xml into Arraylist ? i'm not clear. – himel Oct 12 '17 at 12:16
  • its too complicated :P i hope with kotlin there will not be any big code errors so i am waiting for Android Studio 3.0 then i ll convert my java code to kotlin. Thanks for your reply :) @himel – Murtaza Oct 12 '17 at 13:28
  • You need to understand the logic before you start converting to kotlin. Android Studio will do all of the heavy listing for you. You do not have to do much, just paste your code into a kotlin project (class/file...) that is on android studio 3.0. @Murtaza – letsCode Oct 12 '17 at 16:10
0

If you have a constructor class you can do something like this.

You want to create a model class

public class model {

String s1, s2, s3;

public model(String s1, String s2, String s3) {
    this.s1 = s1;
    this.s2 = s2;
    this.s3 = s3;
}

public String getS1() {
    return s1;
}

public String getS2() {
    return s2;
}

public String getS3() {
    return s3;
}

}

From there you can do something like the following.

List<model> modelList = new ArrayList<>();
modelList.add("1", "2", "3");
// ADD MORE

Then you can create a for loop.

for (int i = 0; i < modelList.size(); i++) {
    System.out.println(modelList.get(i).getS1());    
    System.out.println(modelList.get(i).getS2());    
    System.out.println(modelList.get(i).getS3());
}
//THIS WILL PRINT OUT THE STRINGS IN THE MODEL

have fun

letsCode
  • 2,774
  • 1
  • 13
  • 37
0

You can convert array list to Json and store it in string.
And when you need back the data just reverse it...
Array to json below code

    String mStringArray[] = { "String1", "String2" };

    JSONArray mJSONArray = new.      
   JSONArray(Arrays.asList(mStringArray));

Or you can use the GSON library for the same. http://code.google.com/p/google-gson/

Maraj Hussain
  • 1,580
  • 10
  • 26