Frankie
Frankie

Reputation: 15

Arraylist to string and back to arraylist

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;
}
}

Upvotes: 0

Views: 934

Answers (3)

Athul
Athul

Reputation: 821

//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());

Upvotes: 2

Shiva Snape
Shiva Snape

Reputation: 544

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....

Upvotes: 0

Hemant Parmar
Hemant Parmar

Reputation: 3976

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 ;
}

Upvotes: 0

Related Questions