列表不显示数据

时间:2015-06-07 01:28:27

标签: android arrays listview arraylist parse-platform

我在内部存储中存储了字符串(objectIDs)。当我想显示保存的ID列表时,它可以正常工作,但是当我尝试从Parse.com获取名称时,它不会显示在列表中。 这显示了ObjectIds:

public class UserListForHistory extends ListActivity {

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

    SharedPreferences settings = getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
    Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());

    ListView idList;
    List<String> idListData;
    idListData = new ArrayList<String>();
    idList = (ListView) findViewById(android.R.id.list);

    for(String userIds : myStrings){

        idListData.add(userIds);

    }

    ArrayAdapter<String> str = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, idListData);
    idList.setAdapter(str);


}

然而,当将此添加到“for”时,它不会在列表中显示任何内容!但它确实显示了logcat中的所有名称。有人看到问题吗?

public class UserListForHistory extends ListActivity {

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

    SharedPreferences settings = getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
    Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());

    final ListView idList;
    final List<String> idListData;
    idListData = new ArrayList<>();
    idList = (ListView) findViewById(android.R.id.list);

    for(String userIds : myStrings) {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
        query.getInBackground(userIds, new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject parseObject, ParseException e) {
                String name = parseObject.getString("fullname");
                Log.d("These are the names: ", name);

                idListData.add(name);
            }
        });

    }
    ArrayAdapter<String> str = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, idListData);
    idList.setAdapter(str);


}

1 个答案:

答案 0 :(得分:1)

此for循环中的调用:

for(String userIds : myStrings) {
    query.getInBackground(userIds, new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject parseObject, ParseException e) {
            String name = parseObject.getString("fullname");
            Log.d("These are the names: ", name);

            idListData.add(name);
        }
    });
}

是异步的,这意味着当代码到达以下行时idListData仍为空:

ArrayAdapter<String> str = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, idListData);
idList.setAdapter(str);

要解决此问题,您可以执行以下操作:

final ArrayAdapter<String> str = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, new ArrayList<>());
idList.setAdapter(str);

for(String userIds : myStrings) {
    query.getInBackground(userIds, new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject parseObject, ParseException e) {
            String name = parseObject.getString("fullname");
            Log.d("These are the names: ", name);

            str.add(name);
        }
    });
}

在此代码中,适配器与列表同步关联,然后异步检索的数据一旦可用就会添加到适配器。您的确切实现可能会根据您执行这组查询的频率而有所不同,但上面的代码(未编译,可能包含拼写错误)应该为您说明一般原则。