Java 如何转换arraylist<;对象>;字符串并将其重新转换为arraylist<;对象>;
我正在尝试将arrayList转换为字符串一段时间,并将该字符串转换为arrayList,因为我想将该字符串插入listView,并且该字符串包含一些随机/n,下面是代码:Java 如何转换arraylist<;对象>;字符串并将其重新转换为arraylist<;对象>;,java,android,string,arraylist,Java,Android,String,Arraylist,我正在尝试将arrayList转换为字符串一段时间,并将该字符串转换为arrayList,因为我想将该字符串插入listView,并且该字符串包含一些随机/n,下面是代码: ArrayList<msg> per = new ArrayList<msg>(); String t; t="................................./n........"; msm = new msg(); test(); per.add(msm); MonAdapter
ArrayList<msg> per = new ArrayList<msg>();
String t;
t="................................./n........";
msm = new msg();
test();
per.add(msm);
MonAdapter adapter = new MonAdapter(this, R.layout.list, per);
ArrayList per=new ArrayList();
字符串t;
t=“………/n……”;
msm=新消息();
test();
添加(msm);
MonAdapter adapter=新的MonAdapter(this,R.layout.list,per);
阵列列表:
List<String> list = new ArrayList<>();
for (int i = 0; i < 50; i++)
{
list.add("Item " + i);
}
String[] array = list.split(",");
List<String> listAgain = new ArrayList<String>(Arrays.asList(array));
字符串到ArrayList:
List<String> list = new ArrayList<>();
for (int i = 0; i < 50; i++)
{
list.add("Item " + i);
}
String[] array = list.split(",");
List<String> listAgain = new ArrayList<String>(Arrays.asList(array));
String[]array=list.split(“,”);
listlistreach=newarraylist(Arrays.asList(array));
为什么不使用数组列表?
或者一个ArrayList
,并给对象一个字符串变量,该变量保存要显示的数据
因此,您可以调用array.get(position.dataIwantString()
编辑:
可以创建如下所示的对象:
public class MyObject {
public String myData;
public MyObject(String data){
myData = data
}
}
如果创建ArrayList
,则不必转换数组以获得所需的值
通过调用myArray.add(newmyobject(“hello”))添加一个新的MyObject代码>
并且可以使用MyObject obj=myArray.get(positionInArray)
检索对象。
最后,您可以调用obj.myData
来检索字符串是否要根据分隔符将字符串滑入多个子字符串/n不。您想要的是按原样显示对象,这意味着必须增强适配器才能正确显示它们。例如,与中的自定义布局类似。@haceneabdessamed yeseffectively@zapl我没有理解你所说的。不要将ArrayList
更改为ArrayList
。更改适配器,使其能够正确显示SomeObject
。您不明白您想在列表中显示字符串,对吗?您还可以使用上面的代码通过创建for循环:for(inti=o;i)来创建ArrayList