Skip to content
Advertisement

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

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();
    }

}

Advertisement

Answer

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.

9 People found this is helpful
Advertisement