1

I'm sending a string from one Activity to another Activity with putExtras and getExtras. Then I displaying this string to a TextView. When I go back and choose another string it ovewrites the previous string in TextView. I want to place the new string in a new line in textView. How to do this? (I'm sending strings with a button) This is my code in the recieve Activity:

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_completed_tasks);


    TextView completedTasksView = (TextView)findViewById(R.id.completed_tasks);


    Intent intent = getIntent();

    String completedTasks = intent.getExtras().getString("completedTasks");
    completedTasksView.setText(completedTasks);


}

}
comrade
  • 4,590
  • 5
  • 33
  • 48
  • You can use https://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String,%20int%29 and then html-breaks. – cherry-wave Jun 28 '16 at 13:29
  • You probably want a RecycleView, thats exactly what it is for. It is possible to achieve with a TextView if you really insist though. You concatenate the new value with the old ones (instead of `completedTasksView.setText(completedTasks)` do `completedTasksView.setText(completedTasksView.getText() + completedTask` – Ishay Peled Jun 28 '16 at 13:31

4 Answers4

2

According to: TextView

append(CharSequence text)

Convenience method: Append the specified text to the TextView's display buffer, upgrading it to BufferType.EDITABLE if it was not already editable.

So use:

completedTasksView.append("\n" + completedTasks);
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
  • No.. it didnt worked.. Same thing happened.. Should I do this onResume or something? – Thanos Tokmakis Jun 28 '16 at 13:40
  • @ThanosTokmakis the method is correct, I think that you have to read http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android for correct use of intent between activities – Michele Lacorte Jun 28 '16 at 13:43
  • this is how i send the extras `checkTask.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(TasksActivity.this, CompletedTasks.class); intent.putExtra("completedTasks", tasks.get(0)); startActivity(intent); tasks.remove(tasks.get(0)); } });` – Thanos Tokmakis Jun 28 '16 at 13:46
0

You have to store the string that you sent previously using putExtras because next time activity will be recreated again and your previous data will be lost. To save previous String you can create a static arraylist and on getIntent add your string to arraylist and when you want to print use a for loop and create a single string with appended "/n" and set that string to textview like this For eg.

public static ArrayList<String> list;

To add string to list

list.add(completedTasks);

To create a single string from arraylist

String text="";
for(int i=0;i<list.size();i++)
{
if(text!="")
{
text=text+"\n"+list(i);
}else{
text=list(i);
}
}
textview.setText(text);

NOTE: Arraylist should be static for app and not within activity so define arraylist in different class then activity

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
0

you can create One globle class with one String variable which will store your completedTask Text every time set text when you get new string ...

PriyankaChauhan
  • 953
  • 11
  • 26
0

Create one global String and always append to it:

    String completedTasks = "";

      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_completed_tasks);


        TextView completedTasksView = (TextView)findViewById(R.id.completed_tasks);


        Intent intent = getIntent();

        completedTasks += "\n" + intent.getExtras().getString("completedTasks");
        completedTasksView.setText(completedTasks);


    }

    }
Sagar Jogadia
  • 1,330
  • 1
  • 16
  • 25