2012-08-11 76 views
0

我正在嘗試在移動應用程序上執行一些操作。 我有解析器class: A如何將字符串值存儲到數組字符串,並可以在另一個類中調用該數組以在單行中打印爲字符串

for (int i = 0; i < jArray3.length(); i++) { 
    news_id = Integer.parseInt((json_data.getString("news_id"))); 
    news_title = json_data.getString("news_title"); 
} 

我必須分析和獲取ID和標題的價值。現在我想將這個標題存儲在數組中並調用另一個類。在該類中,我們必須將該數組轉換爲String,以便我可以在單行中打印該標題值。

我該如何實現這個可以爲我發佈一些代碼?

回答

0

基於我對你的問題的理解,我假設只有你正在尋找下面的代碼片段。

String[] parsedData = new String[2]; 
for (int i = 0; i < jArray3.length(); i++) { 
    news_id = Integer.parseInt((json_data.getString("news_id"))); 
news_title = json_data.getString("news_title"); 
} 

parsedData[0] = news_id; 
parsedData[1] = news_title; 

DifferentCls diffCls = new DifferentCls(data); 
System.out.println(diffCls.toString()); 

DifferentCls.java

private String[] data = null; 

public DifferentCls(String[] data) { 
this.data = data; 
} 

public String toString() { 
return data[1]; 
} 
0
news_title = json_data.getString("news_title"); 

Add line after the above line to add parse value int0 string array 

字符串[] NEWROW =新的String [] {news_id,news_title};

//數組轉換爲字符串

String asString = Arrays.toString(newRow) 
0

我假設你有多個標題存儲。

我使用ArrayList<String>這是Array更加靈活。

創建ArrayList和存儲所有標題值:

ArrayList<String> titleArr = new ArrayList<String>(); 

for (int i = 0; i < jArray3.length(); i++) { 
    news_id = Integer.parseInt((json_data.getString("news_id"))); 
    news_title = json_data.getString("news_title"); 

    titleArr.add(news_title); 
} 

2.現在把它發送到another class,你需要顯示在單線所有的冠軍。

new AnotherClass().alistToString(titleArr); 


// This line should be after the for-loop 
// alistToString() is a method in another class   

3.另一類結構。

public class AnotherClass{ 

     //.................. Your code........... 


     StringBuilder sb = new StringBuilder(); 
     String titleStr = new String(); 

    public void alistToString(ArrayList<String> arr){ 

    for (String s : arr){ 

    sb.append(s+"\n"); // Appending each value to StrinBuilder with a space. 

      } 

    titleStr = sb.toString(); // Now here you have the TITLE STRING....ENJOY !! 

     } 

    //.................. Your code............. 

    } 
相關問題