Arraylist to string and back to arraylist

时间:2018-02-05 07:17:05

标签: java android arraylist

我想将(TestObject)的ArrayList转换为String,反之亦然。我已经附加了主要活动,因此您可以看到我想要创建的方法。

MainActivity:制作ArrayList并需要转换为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;
}

}

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

3 个答案:

答案 0 :(得分:1)

// ArrayList to String Convert:

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

// ViceVersa String获取数组列表:

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

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

更新

 myModelList = gson.fromJson(br, new TypeToken<ArrayList< TestObject >>(){}.getType());

答案 1 :(得分:0)

首先用onCreate()方法填充arraylist:

@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)
}

现在调用将arraylist转换为字符串

的方法
private String convertObjectArrayToString(ArrayList<TestObject> arrayToBeConverted){

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

实现字符串到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 :(得分:0)

更改您的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;
        }

    }

将ArrayList转换为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....