2016-05-09 13 views
-3

我想將我的數組列表轉換爲字符串一段時間,並將字符串轉換爲數組列表,因爲我想將字符串插入到列表視圖中並且此字符串包含一些隨機/ N,這裏是代碼:如何將數組列表<object>轉換爲字符串並將其重新轉換爲數組列表<object>

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); 
+0

你想把字符串分成許多基於分隔符的子串/ n –

+0

不要。你想要的是按照原樣顯示對象,這意味着必須增強適配器才能正確顯示它們。例如,像https://guides.codepath.com/android/using-the-recyclerview中的自定義佈局。 – zapl

+0

@haceneabdessamed yes yes –

回答

0

你爲什麼不使用ArrayList<String>? 或ArrayList<Object>並給對象一個字符串變量來保存你想要顯示的數據?

所以你可以撥打array.get(position).dataIWantInString();

編輯:如果你創建你不要有數組轉換爲得到你想要的值ArrayList<MyObject>

public class MyObject { 

    public String myData; 

    public MyObject(String data){ 
    myData = data 
    } 
} 

你可以創建一個對象是這樣的。

通過調用myArray.add(new MyObject("hello")); 可以添加一個新的MyObject,並且可以使用MyObject obj = myArray.get(positionInArray)來檢索該對象。 最後你可以撥打obj.myData來檢索你的字符串

+0

不明白你 –

+0

我更新了我的awnser :) – locomain

+0

它不能解決我的問題:/ –

0

的ArrayList:

List<String> list = new ArrayList<>(); 

     for (int i = 0; i < 50; i++) 
     { 
      list.add("Item " + i); 
     } 

的ArrayList爲String:

String string = list.toString(); 

字符串ArrayList的:

String[] array = list.split(","); 
List<String> listAgain = new ArrayList<String>(Arrays.asList(array)); 
+0

不是一個ArrayList的,ArrayList的

相關問題