Reputation: 425
I am trying to add the contents of my array list to an AlertDialog, but it comes up with error:
java.lang.IllegalStateException: Could not execute method of the activity
Here is my code snippet that I am having problems with:
public void ShowOnlineUserDialog(){
CharSequence[] users = {_onlineUsers.get(1), _onlineUsers.get(2), _onlineUsers.get(3), _onlineUsers.get(4)};
AlertDialog.Builder onlineUser = new AlertDialog.Builder(this);
onlineUser.setTitle("Online Users");
onlineUser.setItems(users, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.out.println("User clicked!");
}
});
onlineUser.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
onlineUser.show();
}
Moreover, is there a more efficient way of adding to the alertbox, for loop maybe? I'm sorry but my lack of knowledge in arrays fails me.
Any help would be amazing! Thank you!
EDIT: If I set my code out this way:
String user1 = _onlineUsers.get(1);
String user2 = _onlineUsers.get(0);
CharSequence[] users = {user1, user2};
It works perfectly, but I would like to find a more efficient way?
Upvotes: 2
Views: 5717
Reputation: 1280
Your code is correct and has no bug. I think you have to check it -
CharSequence[] users = {_onlineUsers.get(1), _onlineUsers.get(2), _onlineUsers.get(3), _onlineUsers.get(4)};
Check that it is able to fetch string from it or not.
Upvotes: 0
Reputation: 23596
You can do something like this:
Take String[] users
instead of the CharSequence[] users
and do like below:
users = new String[_onlineUsers.size()];
System.out.println("Total Item is: "+_onlineUsers.size());
users = _onlineUsers.toArray(users);
System.out.println("USERS :"+_onlineUsers.toArray(users));
That will convert your whole ArrayList<String> _onlineUsers
to String[] users
.
Hope it will help you.
Be Free to ask any question if it is not solve your issue.
Upvotes: 3