用户objectIds未显示在RemoteDataTask中

时间:2016-03-17 21:12:14

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

这里我有一个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

1 个答案:

答案 0 :(得分:0)

我做了一些研究,发现在没有先保存的情况下,您无法从查询中检索用户objectId。

请在下面的答案中查看我的评论。解决方案是修改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);
                }