【问题标题】:List doesn't show data列表不显示数据
【发布时间】:2015-08-21 17:46:40
【问题描述】:

我的内部存储器中存储了字符串(objectID)。当我想显示保存的 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);


}

【问题讨论】:

    标签: android arrays listview arraylist parse-platform


    【解决方案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);
            }
        });
    }
    

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

    【讨论】:

      猜你喜欢
      • 2015-12-15
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多