0

I have a chat program that displays I list of online users. The message back from the server is "RESP_USERLIST,,, etc. Except I am having problems adding these usernames to my list.

Here is my current code:

List <String> responseList = Arrays.asList(OnlineUsersPost.split(","));
    if (responseList.contains("RESP_USERLIST")){
        _onlineUsers = responseList.get(1);

        System.out.println("Online users: " + _onlineUsers);

And where I initialise it:

private String _onlineUsers;

It seems it is only taking the first user and adding it to the list, I want to add them all to an arraylist.

EDIT: I have now tried it this way, with little difference:

List <String> responseList = Arrays.asList(OnlineUsersPost.split(","));
    if (responseList.contains("RESP_USERLIST")){
        for (int i = 0; i < responseList.size(); i++) {
            _onlineUsers.add(responseList.get(i));
            System.out.println("Online users: " + _onlineUsers);
        }

And where I initialise it:

private List<String> _onlineUsers;
5
  • what are you getting, and what is issue, please explain properly Commented Apr 7, 2012 at 4:50
  • The first code only gets the first entry (online user), and the second one crashes my program with "java.lang.IllegalStateException: Could not execute method of the activity" Commented Apr 7, 2012 at 4:58
  • paste log cat as well, to find out why app is crashing Commented Apr 7, 2012 at 4:59
  • 04-07 14:56:36.695: E/AndroidRuntime(362): java.lang.IllegalStateException: Could not execute method of the activity 04-07 14:56:36.695: E/AndroidRuntime(362): at android.view.View$1.onClick(View.java:2144) 04-07 14:56:36.695: E/AndroidRuntime(362): at android.view.View.performClick(View.java:2485) 04-07 14:56:36.695: E/AndroidRuntime(362): at android.view.View$PerformClick.run(View.java:9080) 04-07 14:56:36.695: E/AndroidRuntime(362): at android.os.Handler.handleCallback(Handler.java:587) 04-07 14:56:36.695: E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:92) Commented Apr 7, 2012 at 5:02
  • Should it be responseList.size() in the for loop? I'm having doubts about that part... Commented Apr 7, 2012 at 5:02

1 Answer 1

1

I think You should initialize your arraylist first, initialization statement you are showing is only declaration, so initialize it as:

private List<String> _onlineUsers= new ArrayList<Sting>();

and change your loop to following:

ist <String> responseList = Arrays.asList(OnlineUsersPost.split(","));
    if (responseList.contains("RESP_USERLIST")){
        for (int i = 1; i < responseList.size(); i++) {
            _onlineUsers.add(responseList.get(i));
            System.out.println("Online users: " + _onlineUsers);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly! Thank you so much!

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.