我的代码没有使用Parse Query从parse.com中提取信息

时间:2015-01-05 19:51:31

标签: java android android-intent parse-platform

所以我有一个列表视图,可以满足给定某些条件的所有用户。现在,当我尝试将用户点击用户的项目时,它就像其他任何社交网络一样提取用户个人资料。所以我使用parse.com作为我的后端,我得到了一个Intent从被点击的用户中提取objectId并将其置于意图中的方式

usersList.setAdapter (usersAdapter);

    usersList.setOnItemClickListener (new AdapterView.OnItemClickListener () {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            GamingProfile profile = usersAdapter.getItem (position);

            try {
                profile.getGamer ().getObjectId ();
            } catch (ParseException e) {
                e.printStackTrace ();
            }

            openProfile (profile);

        }
    });


    return v;
}

这是openProfile的方法

 public void openProfile (GamingProfile profile) {

    profile = usersAdapter.getItem (0);

    Intent intent = new Intent (getActivity (), ViewUserProfile.class);

    intent.putExtra ("USERS_ID", profile.getId ());

    startActivity (intent);

    Toast.makeText (getActivity (),
                        "Found user",
                        Toast.LENGTH_SHORT).show ();

        }

但是当我进入ViewUserProfile活动时,没有任何内容加载到字段中。以下是我的代码:

 Intent intent = getIntent ();
    usersId = intent.getStringExtra ("USERS_ID");
    String currentUserId = ParseUser.getCurrentUser ().getObjectId ();


    ParseQuery<ParseObject> query = ParseQuery.getQuery ("GamingProfile");
    query.include ("gamer");
    query.whereNotEqualTo (usersId, currentUserId);
    query.findInBackground(new FindCallback<ParseObject> () {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                // The query was successful.
                for(ParseObject thisUser : objects){
                    lolIdSting = thisUser.getString("leagueOfLegendsId");
                    psnIdString = thisUser.getString("playStationId");
                    xboxIdString = thisUser.getString ("xboxId");

                    try {
                        vuserProfilePictureView.setProfileId (gProfile.getJSONObject ("profile").getString ("facebookId"));
                    } catch (JSONException e1) {
                        e1.printStackTrace ();
                    }

                    try {
                        vuserGenderView.setText (gProfile.getJSONObject ("profile").getString ("name"));
                    } catch (JSONException e1) {
                        e1.printStackTrace ();
                    }

                    try {
                        vuserNameView.setText (gProfile.getJSONObject ("profile").getString ("name"));
                    } catch (JSONException e1) {
                        e1.printStackTrace ();
                    }

                }

                vuserLoLId.setText (lolIdSting);
                vuserPsnId.setText (psnIdString);
                vuserXboxId.setText (xboxIdString);

            } else {
                // Something went wrong.
                Toast.makeText (getApplicationContext (), "Error in getting user data_"
                        + e.getMessage (), Toast.LENGTH_LONG).show ();

                Log.d (GamerzioApplication.TAG, "Error parsing saved user data.");
            }
        }
    });

}

我尝试调试openProfile并且没有找到objectId,所以我猜测我没有从适配器获取objectId但我不知道如何根据项目点击检索它爱好。

public void openProfile (GamingProfile profile) {

    try {
        if (profile.getGamer ().has ("objectId")) {

        Intent intent = new Intent (getActivity (), ViewUserProfile.class);

        intent.putExtra ("USERS_ID", profile.getId ());

        startActivity (intent);

        Toast.makeText (getActivity (),
                "Found user",
                Toast.LENGTH_SHORT).show ();
    } else {

            Toast.makeText (getActivity (), "No objectId", Toast.LENGTH_LONG).show ();
        }
    } catch (ParseException e) {
        e.printStackTrace ();
    }

}

这是我的LIstAdapter

public class UsersAdapter extends ParseQueryAdapter<GamingProfile> {


static ParseUser currentUserId = ParseUser.getCurrentUser ().getParseUser ("gamer");

public static TextView userProfileName;

public UsersAdapter(final Context context) {
    super (context, new QueryFactory<GamingProfile> () {
        @Override
        public ParseQuery<GamingProfile> create() {
            ParseQuery query = ParseQuery.getQuery ("GamingProfile");

            query.whereNotEqualTo ("gamer", currentUserId);

            query.orderByDescending ("gamer");


            return query;
        }
    });
}



@Override
public View getItemView(GamingProfile gamingProfile, View v, ViewGroup parent) {

    if (v == null) {
        v = View.inflate (getContext (), R.layout.user_list_item, null);
    }

    super.getItemView (gamingProfile, v, parent);

    final ProfilePictureView userProfilePictureView = (ProfilePictureView)
            v.findViewById (R.id.profileImageItem);

    try {
        userProfilePictureView.setProfileId (gamingProfile.getGamer ().getJSONObject ("profile").getString ("facebookId"));
    } catch (JSONException | ParseException e) {
        e.printStackTrace ();
    }

     userProfileName = (TextView)
            v.findViewById (R.id.userItemName);


    try {
        userProfileName.setText (gamingProfile.getGamer ().getJSONObject ("profile").getString ("name"));
    } catch (JSONException | ParseException e) {
        e.printStackTrace ();
    }


    return v;
}

}

1 个答案:

答案 0 :(得分:1)

您是否尝试调试代码并确保传递正确的USER_ID? 在 openProfile 中,您传递一个配置文件,然后用

覆盖它
  

profile = usersAdapter.getItem(0);

另外,请确保适配器中的配置文件包含ID。