1

I have an ArrayList that I access in a fragment with the following code:

getData = getActivity().getIntent().getExtras().getString("importEvents");
List<String> myList = new ArrayList<String>(Arrays.asList(getData.split(",")));
myList.remove(0);
myList.remove(myList.size() - 1);

The output of this is as follows

Event1 (2017-03-13) 
Event2 (2017-03-14)

Each event starts on a new line following the second ) but am I able to save everything before the first bracket as a separate string so I can assign it to another TextView in a CardView?

4
  • Let's say your list contains 4 items, you want to build a String with items with index 1 and 2? Please explain. Commented Mar 7, 2017 at 23:07
  • 1
    I'm seeing some problem in your code, the myList.size() will be changed once you remove the first item from the myList. So, store the size of list before you remove the first item Commented Mar 7, 2017 at 23:08
  • @Pons I want to separate the value of the array lists essentially into two parts - the name of the event, and then the date of the event. None of the other methods listed below have worked as yet. Commented Mar 7, 2017 at 23:17
  • I have placed answer for this requirement, please take a look and let me know if you have any questions. Let me know if i haven't understood you clearly Commented Mar 7, 2017 at 23:35

2 Answers 2

1
myList.get(0).substring(0, myList.get(0).indexOf('('))
Sign up to request clarification or add additional context in comments.

4 Comments

I get an error of "cannot resolve method 'subString(int,int)'"
sorry it will be substring
I had tried that fix, but it has no effect on the output. It shows no errors though
it should print only Event1. You should use a TextView to print that value.
0

Please find below some sample to convert as per your requirement,

    String myString = "Event1 (2017-03-13) Event2 (2017-03-14)";
    myString = myString.replaceAll(" \\(", "\\(");

    List<Event> events = Arrays.asList(myString.split(" ")).stream()
            .map(event -> new Event(event.split("\\(")[0], event.split("\\(")[1].replaceAll("\\)", "")))
            .collect(Collectors.toList());

    System.out.println(events);

Create a POJO, Event class,

    public class Event {

private String name;
private String date;

Event(String name, String date) {
    this.name = name;
    this.date = date;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDate() {
    return date;
}
public void setDate(String date) {
    this.date = date;
}

public String toString() {
    return this.name + "\t" + this.date;
}

}

Sample Output

[Event Name - Event1 Date - 2017-03-13 , Event Name - Event2 Date - 2017-03-14 ]

This will give you an idea of how to split your string in to event and event date

2 Comments

I only used two events as an example, the overall string is actual google calendar events. I access it through the link and then extract the final value as the Array. Would I assign the 'String myString' to be the final value retrieved from the calendar and process it like that?
@smithcommajohn I have made some changes. yes, you can assign the final value to myString. I have create another POJO Event to store the event information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.