简体   繁体   English

JSON字符串Java中的换行

[英]Break line in json string java

Don't know how to express myslef very well.As you see down in my code I have a String,an ArrayList of Strings and a spinner. 不知道如何很好地表达myslef。正如您在我的代码中看到的那样,我有一个字符串,一个字符串数组列表和一个微调器。

In my code I add the elements of my String in my ArrayList and then in my spinner. 在我的代码中,我在ArrayList中然后在微调器中添加String的元素。 All good,except the fact that everything is on a single line,and I want each of the elements ("one,two,three,four") in a new line. 一切都好,除了所有内容都在同一行上,而且我希望每个元素(“一个,两个,三个,四个”)都换行。

Note: I do not know what String accounts contain and how many values it has in it. 注意:我不知道String帐户包含哪些内容以及其中包含多少个值。

ArrayList<String> spinnerAccounts = new ArrayList<>();
String accounts = "one two three four";
Spinner accounts = (Spinner)findViewById(R.id.accounts);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinnerAccounts);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        accounts.setAdapter(adapter);
spinnerAccounts.add(System.lineSeparator());
                spinnerAccounts.add(accountID.toString());

Try below code. 尝试下面的代码。

 ArrayList<String> spinnerAccounts = new ArrayList<>(); // I don't know the use of this ArrayList
 Spinner accounts = (Spinner)findViewById(R.id.accounts);
 ArrayAdapter<String> adapter =
       new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinnerAccounts);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 accounts.setAdapter(adapter);
 spinnerAccounts.add("one");
 spinnerAccounts.add("two");
 spinnerAccounts.add("three");
 spinnerAccounts.add("four");
 adapter.notifyDataSetChanged();

Add items you want to display in Spinner using spinnerAccounts.add("ACCOUNT_NAME") method. 使用spinnerAccounts.add("ACCOUNT_NAME")方法添加要在Spinner显示的项目。

Hope It'll help you. 希望对您有帮助。

Modify your string which will be shown in spinner like this 修改将在微调器中显示的字符串,如下所示

StringBuffer accounts = new StringBuffer("one");
stringBuffer.append("\n");
stringBuffer.append("two");
stringBuffer.append("\n");
stringBuffer.append("three");
stringBuffer.append("\n");
stringBuffer.append("four");

TextView in the spinner will populate the values in different lines provided height and width is wrap_content 如果高度和宽度为wrap_content ,则微调器中的TextView将在不同的行中填充值

Change ur code "accounts" used two times. 两次更改您的代码“帐户”。 Make arraylist for accounts and load into spinner. 为帐户创建arraylist并加载到微调器中。

    Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    List<String> accountsList = new ArrayList<String>();
    accountsList.add("one");
    accountsList.add("two");
    accountsList.add("three");
    accountsList.add("four");

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                 (this,android.R.layout.simple_spinner_item,accountsList);

    dataAdapter.setDropDownViewResource
                 (android.R.layout.simple_spinner_dropdown_item);

    spinner1.setAdapter(dataAdapter);

    // Spinner item selection Listener  
    addListenerOnSpinnerItemSelection();

    // Button click Listener 
    addListenerOnButton();

Follow this Tutorial 遵循本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM