android 如何将字符串值存储到数组String中,并可以调用另一个类中数组以在单行中打印为字符串

anauzrmj  于 2023-01-15  发布在  Android
关注(0)|答案(3)|浏览(113)

我正在尝试做一些移动的应用程序的工作。我在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和title的值,现在我想把这个title存储在数组中,并调用另一个类,在那个类中,我们必须把那个数组转换成String,这样我就可以在一行中打印title的值。
我怎样才能实现这一点你可以张贴一些代码给我吗?

x8diyxa7

x8diyxa71#

根据我对你的问题的理解,我假设下面的片段只有你在寻找。

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];
}
ehxuflar

ehxuflar2#

news_title = json_data.getString("news_title");

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

字符串[] newRow =新字符串[] {新闻标识,新闻标题};

//将数组转换为字符串

String asString = Arrays.toString(newRow )
puruo6ea

puruo6ea3#

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,您需要在其中以单行显示所有标题。

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

   }

相关问题