How to get data from another class using ParseQueryAdapter (Android + Parse)

I'm trying to display the username and a photo in a a list view using the ParseQueryAdapter. I have the classes: User and Post.

I set it up here:

String[] postsObjectIds = some_string_array

ParseQueryAdapter<Post> adapter = new ParseQueryAdapter<Post>(getActivity(), new ParseQueryAdapter.QueryFactory<Post>() {
        public ParseQuery<Post> create() {
            ParseQuery<Post> query = Post.getQuery();
            query.include("User");
            query.whereContainedIn("objectId", Arrays.asList(postsObjectIds));
            return query;
        }
});

adapter.setTextKey("username");
adapter.setImageKey("photo");
adapter.setPlaceholder(getResources().getDrawable(R.drawable.some_drawable));

ListView listView = (ListView) rootView.findViewById(R.id.post_list_view);
listView.setAdapter(adapter);

The Post class has a column called photo and the User class has a column called username. The list view isn't displaying the username.

Do I have to implement a custom adapter? Is there a quick fix?

Answers


I found an answer on the Parse Help Forum:

ParseQueryAdapter setTextKey for relational object

The accepted answer says to use a custom ParseQueryAdapter<T> and Override the getItemView.

For example, in my Fragment's onCreateView method, I initialized the adapter and set it (postsObjectIds is some String[]):

PostViewListViewAdapter adapter = new PostViewListViewAdapter(getActivity(), postsObjectIds);

ListView listView = (ListView) rootView.findViewById(R.id.post_list_view);
listView.setAdapter(adapter);

Here's my adapter (PostViewListViewAdapter):

public class PostViewListViewAdapter extends ParseQueryAdapter<Post> {

    private final String[] mPostsObjectIds;

    public PostViewListViewAdapter(Context context, final String[] postsObjectIds) {
        super(context, new ParseQueryAdapter.QueryFactory<Post>(){
            public ParseQuery<Post> create() {
                ParseQuery<Post> query = Post.getQuery();
                query.include("User");

                query.whereContainedIn("objectId", Arrays.asList(postsObjectIds));
                return query;
            }
        });
        this.mPostsObjectIds = postsObjectIds;
    }

    @Override
    public View getItemView(Post post, View v, ViewGroup parent) {
        if(v == null) {
            v = View.inflate(getContext(), R.layout.list_item_post_view, null);
        }

        super.getItemView(post, v, parent);

        final TextView usernameTextView = (TextView) v.findViewById(R.id.username_text_view);

        post.getCreator().fetchIfNeededInBackground(new GetCallback<ParseUser>() {
            @Override
            public void done(ParseUser user, ParseException e) {
                usernameTextView.setText(user.getUsername());
            }
        });
        return v;
    }
}

Need Your Help

function with templates and boost

c++ templates boost functor boost-bind

I try to write a functor to call a boost function with bind and some template. So i have this main :

how to sort word count by value in hadoop?

hadoop mapreduce bigdata partitioner

hi i wanted to learn how to sort the word count by value in hadoop.i know hadoop takes of sorting keys, but not by values.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.