1

I am looking to get the names of a few users from the parse.com DB based on a few queries, for which I am using a for loop to get users and load it into a listview using a custom adapter and the code I have used is as under :

query.findInBackground(new FindCallback<ParseUser>() {

    @Override
    public void done(List<ParseUser> pusers, ParseException e) {
        // TODO Auto-generated method stub
        if (e==null) {
            for (ParseUser pu : pusers) {
                Users1[] data = new Users1[] {
                    new Users1(pu.getString("Name"), true)
                };
            }
            UserListAdapter mUserAdapter = new UserListAdapter(getBaseContext(), R.layout.user_list_item, data);                        
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(ManualInviteActivity.this);
            builder.setMessage(R.string.login_error_message);
            builder.setTitle(R.string.login_error_title);
            builder.setPositiveButton(android.R.string.ok, null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    }
});

I am getting an error when I try to initialize the adapter at 'data' which says that data cannot be resolved to a variable, when I have created it already. How do I fix this? Thanks

2 Answers 2

2

What are you doing wrong is, initialising the array each time it enters the loop, i.e.

for(ParseUser pu : pusers)
{
   Users1[] data = new Users1[]
   {
         new Users1(pu.getString("Name"), true) // here every time new instance is created.
   }
}

So instead of this, initialise it outside the for loop -

Users1[] data = new Users1 [pusers.size()];

then start your for loop

int i = 0;
for(ParseUser pu : pusers)
{
   Users1 user=new Users1(pu.getString("Name"), true);
   data[i]= user;
   i++;     
}
UserListAdapter mUserAdapter = new UserListAdapter(getBaseContext(), R.layout.user_list_item, data);
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of below code:

                       if(e==null)
                        {
                            for(ParseUser pu : pusers)
                            {
                                Users1[] data = new Users1[]
                                        {
                                        new Users1(pu.getString("Name"), true)
                                        };
                            }
                            UserListAdapter mUserAdapter = new UserListAdapter(getBaseContext(), R.layout.user_list_item, data);

                        }

Use this corrected code:

                         if(e==null)
                            {
                            Users1[] data = null;
                            int i=0;  

                            for(ParseUser pu : pusers)
                            {

                                  if(data==null){
                                     data = new Users1[pusers.size()];
                                   }                                            

                                   Users1 user=new Users1(pu.getString("Name"), true);
                                   data[i]= user;
                                   i++;     
                            }
                            UserListAdapter mUserAdapter = new UserListAdapter(getBaseContext(), R.layout.user_list_item, data);

                        }

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.