无法在新的FindCallback <parseuser>中的done函数内添加ArrayList <string>

时间:2017-05-26 03:37:34

标签: java android parsing arraylist parse-platform

public class UserActivity extends AppCompatActivity {

    ArrayAdapter arrayAdapter;
    ListView listView;


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

        listView = (ListView) findViewById(R.id.listView);
        final ArrayList<String> userList;
        {
            userList = new ArrayList<>();
        }
        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, userList);

        ParseQuery<ParseUser> query = ParseUser.getQuery();

        // findInBackground tries to find it amongst all the items in the query while getInBackground has two param where you are expected to know the ID of the item
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> objects, ParseException e) {
                if (e == null) {
                    for (ParseUser user : objects) {
                        Log.i("usernames", user.getUsername());
                        userList.add(user.getUsername());
                    }
                }
                else {
                    Log.i("Error", e.getMessage());
                }
            }
        });

        for (int i = 0; i < userList.size(); i++) {
            Log.i("userList", userList.get(i));
        }

        if (userList.isEmpty()) {
            Log.i("userList", "WTF HOW?");
        }

        // Set the listView
        arrayAdapter.notifyDataSetChanged();
        listView.setAdapter(arrayAdapter);
    }
}

出于某种原因,&#34; userList&#34;总是记录&#34; WTF HOW&#34;当userList为空时打印。但是,&#34;用户名&#34;能够正确记录解析服务器中的所有user.getUsernames。我看了Parse findInBackground doesn't add to global arraylist?,它有一个类似的问题,但答案对我没有帮助。我在这做什么使userList变为空?我首先尝试将userList作为全局变量,但这也没有用。

0 个答案:

没有答案