0

So in my code below, line marked with *** gives me the exception

java.util.arraylist cannot be cast to java.lang.object[]

I am trying to retrieve a list of users from backendless. I have created a list view that works when normally adding to list. but not when trying to get the response(Object)

public class UserRolesActivity extends AppCompatActivity {

    ListView userListView;
    TextView changeRoleTv;
    List<String> list;

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

        userListView = (ListView) findViewById(R.id.usersListView);
        list = new ArrayList<>();
        changeRoleTv = (TextView) findViewById(R.id.chooseUserChangeTv);

        Backendless.Data.mapTableToClass("Users", Users.class);

        list.add("Name1");
        list.add("Name2");

        RetrieveUsers retrieveUsers = RetrieveUsers.getInstance();

        retrieveUsers.getUsersAsync(new AsyncCallback<Object>() {
            @Override
            public void handleResponse(Object response) {

                try{
                 ***   Object [] result = (Object []) response;   ***
                    List<Object> user = Arrays.asList(result);

                    for (int i = 0; i< user.size(); i++){

                        if (user.get(i) != null){
                            list.add(((Learner) user.get(i)).getFirstName().toString() + ": " + ((Learner) user.get(i)).getLastName().toString());
                        }
                    }

                }catch (Exception e){
                    ShowToast(e.getMessage().toString());
                }

                userListView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list));


            }

            @Override
            public void handleFault(BackendlessFault fault) {
                ShowToast(fault.getMessage());
            }
        });

    }

    private  void ShowToast(String message)
    {
        Toast.makeText(this, message, LENGTH_LONG).show();
    }

}
1
  • If you really need an array: ((List<?>) response).toArray(). But it's a List, and you wrap the array into a List. So List<Object> user = (List<Object>) result; would work. Commented Aug 31, 2017 at 15:23

4 Answers 4

1

response is of type java.uti.ArrayList wrapped under Object type. You can not cast
Arraylist to Array of Object.

Sign up to request clarification or add additional context in comments.

2 Comments

So what is the best way to get the User Objects from the response object?
Possible option could be updating method getUsersAsync to accepts new AsyncCallback<List<Users>() { @Override public void handleResponse(<List<Users>response) { -- // Not sure you have access to method Other Option could Cast Object directly to List<Users> List<Object> user = List<Object>(response); Note: assuming your response actual type is List<Users>.
0

what you are doing looks a bit long winded:

***   Object [] result = (Object []) response;   ***
List<Object> user = Arrays.asList(result);

    for (int i = 0; i< user.size(); i++){

       if (user.get(i) != null){
           list.add(((Learner) user.get(i)).getFirstName().toString() + ": " + ((Learner) user.get(i)).getLastName().toString());
       }
    }

could be condenced to :

List<?> users = (List<?>)response;
for (Object user : users) {
    if (user != null){
        // Some stuff
    }
}

Next you will get an issue with:

       list.add(((Learner) user.get(i)).getFirstName().toString() + ": " + ((Learner) user.get(i)).getLastName().toString());

because user.get(i)).getFirstName().toString() + ": " is a String which you are then casting to Type Learner. You will need to rethink this.

1 Comment

Thanks, didn't solve everything but helped alot. Yeah the Learner Cast at the bottom was from trying another class and table. I noticed it as soon as I posted.
0

you can define your result as

Object[] arr = new Object[]{...};

Comments

0

To convert List<Object> to array of objects use .toArray() method:

Object[] result = response.toArray();

That's the solution to the original exception.

Comments

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.