简体   繁体   English

Arraylist转换为字符串并返回到arraylist

[英]Arraylist to string and back to arraylist

I want to convert the ArrayList of (TestObject) to a String and vice versa. 我想将(TestObject)的ArrayList转换为String,反之亦然。 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. 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;
}

}

Object in ArrayList: 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;
}
}

//ArrayList to String Convert : // ArrayList转换为String Convert:

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

//ViceVersa String to get Array List : // ViceVersa String获取数组列表:

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

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

Update: 更新:

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

First fill arraylist in onCreate() method: 首先在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)
}

now call method for convert arraylist to string 现在调用将arraylist转换为字符串的方法

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 实现字符串到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 ;
}

Change Your DataClass : TestObject 更改您的数据类: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转换为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....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM