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そのタイトル値を 1 行で出力できるようにする必要があります。

これを実装するにはどうすればよいですか?コードを投稿できますか?

4

3 に答える 3

0

1.複数のタイトルを保存することを想定しています。

より柔軟な方を使用し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。すべてのタイトルを 1 行で表示する必要があります。

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.............

   }
于 2012-08-11T10:05:46.887 に答える
0
 news_title = json_data.getString("news_title");

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

String[] newRow = new String[] {news_id ,news_title};

//配列を文字列に変換

  String asString = Arrays.toString(newRow ) 
于 2012-08-11T09:13:45.313 に答える
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];
}
于 2012-08-11T07:25:39.717 に答える