ArrayList自动变为空

时间:2016-04-20 20:21:18

标签: android parse-platform

当应用运行时,它会提取存储在解析服务器上的用户,然后将其添加到userList。但是,当我尝试使用listView打印相同的列表时,listView为空。

我尝试放置日志并发现userList包含findInBackground方法中的对象但在结束后,userList的大小变为0.请帮我检索用户'列出并显示出来。

public class Users extends AppCompatActivity {
    List<String> userList = new ArrayList<String>();

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

        Intent TimeTableCalled =  getIntent();
        ParseAnalytics.trackAppOpenedInBackground(getIntent());
        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> objects, ParseException e) {
                if(e==null){
                    for(ParseObject object:objects){
                        Log.i("User", object.get("Name").toString());
                        userList.add(object.get("Name").toString());
                        Log.i("size in loop", String.valueOf(userList.size()));
                    }
                    Log.i("size out of loop", String.valueOf(userList.size()));
                }
                Log.i("size outside if", String.valueOf(userList.size()));
            }
        });

        Log.i("size", String.valueOf(userList.size()));
        ListView listView=(ListView) findViewById(R.id.Listview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,userList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String userClicked = parent.getItemAtPosition(position).toString();
                Toast.makeText(getApplicationContext(), "Clicked item is " + userClicked, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

findInBackground创建一个新的后台线程。列表视图和适配器位于主线程上。这就是为什么你的userList变量仍然是空的,它在主线程上,而另一个线程还没有完成它的工作。只需修改像乔治所说的代码,看看会发生什么。

答案 1 :(得分:-1)

findInBackground方法在后台线程上执行,因此此调用之后的onCreate()中的代码实际上在回调done()中的代码之前执行,该代码仅在查询之后执行完成。

要使用查询中的内容更新listView,请在adapter.notifyDataSetChanged()方法的末尾调用done()。这需要稍微重组onCreate(),以便可以在done()中引用适配器。

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

    ListView listView=(ListView) findViewById(R.id.Listview);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,userList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String userClicked = parent.getItemAtPosition(position).toString();
            Toast.makeText(getApplicationContext(), "Clicked item is " + userClicked, Toast.LENGTH_SHORT).show();
        }
    });

    Intent TimeTableCalled =  getIntent();
    ParseAnalytics.trackAppOpenedInBackground(getIntent());
    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> objects, ParseException e) {
            if(e==null){
                for(ParseObject object:objects){
                    Log.i("User", object.get("Name").toString());
                    userList.add(object.get("Name").toString());
                }
                adapter.notifyDataSetChanged();
            }
        }
    });
}