0

I want to convert the ArrayList of (TestObject) to a String and vice versa. I have attached the main activity as well so you can see the methods I want to create.

MainActivity: Where ArrayList is made and needs to be converted to a String.

public class MainActivity extends AppCompatActivity {
ArrayList<TestObject> testObjects;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    testObjects = new ArrayList<>();
    testObjects.add(new TestObject("Name1", "Attribute1"));
    testObjects.add(new TestObject("Example", "Example"));
}


private String convertObjectArrayToString(ArrayList<TestObject> arrayToBeConverted){
    return null;
}

private ArrayList<TestObject> convertStringToObjectArray(){

    return null;
}

}

Object in ArrayList:

public class TestObject {

private String name;
private String attribute;

TestObject(String name, String attribute){
    this.name = name;
    this.attribute = attribute;
}

public void setAttribute(String attribute) {
    this.attribute = attribute;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public String getAttribute() {
    return attribute;
}
}
5
  • where you implement both method? Commented Feb 5, 2018 at 7:19
  • Methods are in the main activity (converObjectArrayToString and convertStringToObjectArray). Commented Feb 5, 2018 at 7:20
  • Add getters and setters to your TestObject class Commented Feb 5, 2018 at 7:21
  • @CagriYalcin added them to the code now Commented Feb 5, 2018 at 7:22
  • could you share the sample example ? Commented Feb 5, 2018 at 9:10

3 Answers 3

2

//ArrayList to String Convert :

String listToJson = new Gson().toJson(testObjects);

//ViceVersa String to get Array List :

Type listType = new TypeToken<List>() {}.getType();

List myModelList = new Gson().fromJson(listToJson, listType);

Update:

 myModelList = gson.fromJson(br, new TypeToken<ArrayList< TestObject >>(){}.getType());
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I tried converting back but i get [{attribute=Attribute1, name=Name1}, {attribute=Example, name=Example}] as the response. How would create it back to TestObject form?
ArrayList to String Convert That is JSON not String. Both are different things
0

First fill arraylist in onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    testObjects = new ArrayList<>();
    testObjects.add(new TestObject("Name1", "Attribute1"));
    testObjects.add(new TestObject("Example", "Example"));
    convertObjectArrayToString(testObjects)
}

now call method for convert arraylist to string

private String convertObjectArrayToString(ArrayList<TestObject> arrayToBeConverted){

String listString = "";
for (String s : arrayToBeConverted)
{
    listString += s + "\t";
}
System.out.println(listString);
return listString;
}

implement method for string to arraylist

private ArrayList<TestObject> convertStringToObjectArray(){
Gson gson = new Gson();
TypeToken<ArrayList<Publication>> token = new TypeToken<ArrayList<TestObject>>() {
    };
ArrayList<TestObject> pb = gson.fromJson(str, token.getType());
    return testObjects ;
}

2 Comments

Hi, I like the code, I haven't used GSON a lot but I am having a few problems. The first problem is the for loop, it wants me to change it to an object instead of a string, do you have any suggestions for that. The other problem is the TypeTokens, should that be TestObject, because it doesn't recognise the variable.
well you can map arraylist to your custom object, and TypeToken is class of Gson.
0

Change Your DataClass : TestObject

    public class TestObject {

    private String name;
    private String attribute;

    TestObject(String name, String attribute){
        this.name = name;
        this.attribute = attribute;
    }

    public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    public String getAttribute() {
            return attribute;
        }

        public void setAttribute(String attribute) {
            this.attribute = attribute;
        }

    }

For Converting ArrayList in to String :

    ArrayList<TestObject> testObjects;
     testObjects = new ArrayList<>();
     testObjects.add(new TestObject("Name1", "Attribute1"));   
     testObjects.add(new TestObject("Example", "Example"));

            for (int i=0;i<testObjects.size();i++)
            {

               String name = testObjects.get(i).getName();
               String attribute = testObjects.get(i).getAttribute();

               System.out.println(name);
               System.out.println(attribute);

            }

    You will get the items in the array list as Strings....

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.