Warm tip: This article is reproduced from stackoverflow.com, please click
android listview android-intent android-adapter

System.getProperty("line.separator") and " \n " Causes listview not to populate

发布于 2020-03-27 15:43:03

In my app I have a List view,

The view is populated by an array of strings

Here is the code and explanation to help you guys

So the string is

StaffList= Client + " Rates: " + Rates + " " + Staff + " Budget Hours: " + BudgetHoursPassData + " Member Budget: " + MemberBudgetPassData + "Notes" + NotesPassData;

That String is then added to an Array

StaffArray.add(StaffList);

This Array is then passed using intents to another activity and displayed in the list view

public  void PassData(){
    Intent GoToReport=new Intent(getApplicationContext(), createProject.class);
    MemberBudgetPassData= MemberBudget.getText().toString();
    BudgetHoursPassData = budgetHours.getText().toString();
    NotesPassData= Notes.getText().toString();

    Toasty.info(popupActivity.this, StaffArray.toString(),Toasty.LENGTH_LONG).show();

    GoToReport.putStringArrayListExtra("Array",StaffArray);
    startActivity(GoToReport);
    Animatoo.animateCard(popupActivity.this);
}

Here is the adapter

final ArrayAdapter arrayAdapter =new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Array);

if (Array == null){
    Toasty.info(createProject.this,"Select a Staff Memebr",Toasty.LENGTH_LONG).show();
}else{
    StaffListView.setAdapter(arrayAdapter);
}

Now You can go back to the same activity and add a second Item to the array

public  void DialogBuilder(){
    Intent GoToReport=new Intent(getApplicationContext(), popupActivity.class);
    GoToReport.putStringArrayListExtra("Array", Array);
    startActivity(GoToReport);
    Animatoo.animateCard(createProject.this);
}

So Now My problem I am having is that The list populates fine with the above code

But As Soon As I add a System.getProperty("line.separator") or " \n " to the string like

StaffList= Client + "\n"+ " Rates: " + Rates + "\n"+ " " + Staff + "\n"+ " Budget Hours: " + BudgetHoursPassData + "\n"+ " Member Budget: " + MemberBudgetPassData + "\n"+ "Notes" + NotesPassData;

The second/third etc. item in the list does not populate, So only the first Item is displayed in the List BUT without the /n or System.getProperty("line.separator") All the items are displayed

Questioner
Ruben Meiring
Viewed
30
Ruben Meiring 2020-02-03 17:14

I solved this By changing the Listview to A Spinner and calling a spinner adapter

  StaffList.setAdapter(new ArrayAdapter<>(createProject.this, android.R.layout.simple_spinner_dropdown_item, Array));