Как разобрать две строки в одно текстовое представление - PullRequest
0 голосов
/ 03 июня 2018

Я хочу, чтобы мой TextView содержал 2 строки данных JSON.

example.json

{
 "volumeInfo": {
   "title": "Computer Architecture",
   "subTitle": "A Quantitative Approach"
}

Итак, из этого JSON я хочу, чтобы мой TextView был: Архитектура компьютера:Количественный подход .Как я должен сделать?Спасибо.

Я хочу использовать только один TextView

Ответы [ 3 ]

0 голосов
/ 03 июня 2018
//Please check the below code will help you as you want to set the text.

String response = "{" +
        " \"volumeInfo\": {" +
        "   \"title\": \"Computer Architecture\"," +
        "   \"subTitle\": \"A Quantitative Approach\"}" +
        "}";   
 try {
        String title = "", subTitle = "";
        JSONObject jsonObject = new JSONObject(response);
        if (jsonObject.has("volumeInfo")) {
            JSONObject jsonObject1 = jsonObject.getJSONObject("volumeInfo");
            if (jsonObject1.has("title")) {
                title = jsonObject1.getString("title");
            }
            if (jsonObject1.has("subTitle")) {
                subTitle = jsonObject1.getString("subTitle");
            }
            String combineString = title + ": " + subTitle;
            TextView textView = findViewById(R.id.textView);
            textView.setText(combineString);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
0 голосов
/ 03 июня 2018

Просто сделай это

String title = "Computer Architecture";
String subtitle = "A Quantitative Approach";
TextView.setText(title + ": " + subtitle);
0 голосов
/ 03 июня 2018

Вы можете объединить строки и затем установить его на TextView, как показано ниже:

 String title = "Computer Architecture";
 String subtitle = "A Quantitative Approach";
 yourTextView.setText(title + "," + subtitle);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...