通过Java指针检索parseUser

时间:2015-01-01 08:22:54

标签: java android parse-platform

我正在尝试显示向应用发布内容的用户,我已经能够检索帖子的内容而不是用户。但是用户正在保存,因为在我的IOS版本中,我可以清楚地看到我为测试App而发布的帖子。我需要显示用户,但user Pointer<_user>不保存为字符串。

我如何获得指针后面的用户名?

IOS版本:

var findUser:PFQuery = PFUser.query()
    findUser.whereKey("objectId", equalTo: posts.objectForKey("user").objectId) 

帖子课程:

@ParseClassName("Posts")

public class Posts扩展ParseObject {

public ParseUser getUser() {

    return getParseUser("user");
}

public void setUser(ParseUser value) {

    put("user", value);
}


public String getContent(){

    return getString("content");
}

public void setContent(String content){

    put("content", content);
}

@Override
public String toString(){
    return getString("user") + "\n" + getString("content");
}

}

列出活动:

List<Posts> posts = new ArrayList<Posts>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.parse_list);


    ParseQuery<Posts> query = new ParseQuery<Posts>("Posts");
    query.orderByDescending("createdAt");
    query.findInBackground(new FindCallback<Posts>() {
        @Override
        public void done(List<Posts> list, ParseException e) {
            if (e != null) {
                Toast.makeText(ParseListActivity.this, "Error " + e, Toast.LENGTH_SHORT).show();
            }
            for (Posts post : list) {
                Posts newPost = new Posts();
                newPost.setUser(post.getUser());
                newPost.setContent(post.getContent());
                posts.add(newPost);
            }

            ArrayAdapter<Posts> adapter = new ArrayAdapter<Posts>(ParseListActivity.this, android.R.layout.simple_list_item_1, posts);
            setListAdapter(adapter);

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

/*
 * Creating posts and refreshing the list will be controlled from the Action
 * Bar.
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.action_refresh: {
            updatePostList();
            break;
        }

        case R.id.action_new: {
            newPost();
            break;
        }
    }
    return super.onOptionsItemSelected(item);
}
private void newPost() {
    Intent i = new Intent(this, newPost.class);
    startActivityForResult(i, 0);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        // If a new post has been added, update
        // the list of posts
        updatePostList();
    }
}


private void updatePostList() {
    ParseQuery<Posts> query = new ParseQuery<Posts>("Posts");
    query.orderByDescending("createdAt");
    query.findInBackground(new FindCallback<Posts>() {
        @Override
        public void done(List<Posts> list, ParseException e) {
            if (e != null) {
                Toast.makeText(ParseListActivity.this, "Error " + e, Toast.LENGTH_SHORT).show();
            }
            for (Posts post : list) {
                Posts newPost = new Posts();
                newPost.setContent(post.getContent());
                posts.add(newPost);
            }

            ArrayAdapter<Posts> adapter = new ArrayAdapter<Posts>(ParseListActivity.this, android.R.layout.simple_list_item_1, posts);
            setListAdapter(adapter);

        }
    });
}

}

enter image description here

0 个答案:

没有答案