【问题标题】:User objectIds are not displaying in RemoteDataTask用户对象 ID 未显示在 RemoteDataTask 中
【发布时间】:2016-03-17 21:12:13
【问题描述】:

在这里,我有一个 AsyncTask,它从我的服务器中提取数据并将其发布到列表中。相当简单的东西。

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            // Create a progressdialog
            mProgressDialog = new ProgressDialog(SearchActivity.this);

            // Set progressdialog message
            mProgressDialog.setMessage("Loading Results...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setCancelable(false);
            mProgressDialog.setCanceledOnTouchOutside(false);

            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            // Create the array
            worldpopulationlist = new ArrayList<SuggestedUser>();
            try {

                final ParseQuery<ParseUser> query = ParseUser.getQuery();
                ob = query.find();
                Collections.shuffle(ob);

                for (ParseUser user : ob) {

                    ParseFile profilePicture = (ParseFile) user.get("profilePicture");
                    SuggestedUser map = new SuggestedUser();
                    map.setName((String) user.get("name"));
                    map.setUsername((String) user.get("username"));

                    if(profilePicture != null) {
                        map.setProfilePicture(profilePicture.getUrl());
                    }

                    map.setObjectId((String) user.get("objectId"));
                    worldpopulationlist.add(map);
                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.search_listview);

            // Pass the results into ListViewAdapter.java
            adapter = new SearchListViewAdapter(SearchActivity.this,
                    worldpopulationlist);

            // Binds the Adapter to the ListView
            listview.setAdapter(adapter);

            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }

在我的适配器中,我遇到了 objectIds 被吐出为 null 的问题。这里可能出了什么问题?其他一切都有效,包括 profilePicture、名称和用户名。为什么没有从列表中删除它们?

//TODO Why aren't these being populated now?
holder.objectId = worldpopulationlist.get(position).getObjectId();
System.out.println(holder.objectId);

这是我的 SuggestedUser 类:

public class SuggestedUser {
    private String name;
    private String profilePicture;
    private String username;
    private String objectId;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getProfilePicture() {
        return profilePicture;
    }

    public void setProfilePicture(String profilePicture) {
        this.profilePicture = profilePicture;
    }

    public String getObjectId() { return objectId; }

    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }

这是我在控制台中得到的:

03-17 14:05:41.582 13238-13238/com.test.app I/System.out: null
03-17 14:05:41.589 13238-13238/com.test.app I/System.out: null
03-17 14:05:41.597 13238-13238/com.test.app I/System.out: null
03-17 14:05:41.604 13238-13238/com.test.app I/System.out: null
03-17 14:05:41.611 13238-13238/com.test.app I/System.out: null
03-17 14:05:41.617 13238-13238/com.test.app I/System.out: null

【问题讨论】:

  • 尝试在map.setObjectId((String) user.get("objectId"));
  • 谢谢!我实际上只是尝试过,这似乎是一种特殊情况,如果不先保存它所引用的对象,就无法将 objectId 打印为字符串。
  • 保存对象是否有助于解决您的问题??

标签: java android listview parse-platform android-asynctask


【解决方案1】:

我做了一些研究,发现如果不先保存它,就无法从查询中检索用户 objectId。

在下面的答案中查看我的 cmets。解决方法是修改 doInBackground() 函数如下:

for (ParseUser user : ob) {

                    // Save the object to make sure the objectId is generated!
                    user.save();

                    ParseFile profilePicture = (ParseFile) user.get("profilePicture");
                    SuggestedUser map = new SuggestedUser();
                    map.setName((String) user.get("name"));
                    map.setUsername((String) user.get("username"));

                    if(profilePicture != null) {
                        map.setProfilePicture(profilePicture.getUrl());
                    }

                    // Do not retrieve the objectId as a string, rather use the built-in Parse request, i.e. getObjectId();.
                    map.setObjectId((String) user.getObjectId());
                    worldpopulationlist.add(map);
                }

【讨论】:

    猜你喜欢
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2018-04-26
    • 2021-07-30
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多