2

在我的应用程序中,我试图创建一个与 CurrentUser 相关的好友列表,我尝试查看 Parse.com 的文档,并在 Parse.com 上询问了一个关于此的问题,并且 Parser 建议我以表格形式进行操作一个数组列。我已经这样做了,尽管它似乎与 CurrentUser 相关(我创建了 3 个帐户并让他们彼此成为朋友,并且这 3 个帐户具有从 ListView 检索到的不同数组列)这不是我要找的因为它只是帐户的用户名,而不是他们个人的“行”来对用户进行操作,如果你知道我的意思的话。

所以我要问的问题是,使用 Parse 后端数据库根据 CurrentUser 方法创建用户“好友列表”的最佳做法是什么?

这是我的 2 个活动,其中 FindFriends 是我搜索用户并将它们添加到“朋友列表”的地方,而在 PlayAFriend 中,朋友用户将通过 AdapterArray 列表视图加载。

找朋友班

package com.fullfrontalgames.numberfighter;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.urbanairship.UAirship;

public class Findfriends extends Activity {

    protected static final String TAG = null;

    ParseObject po;

    @Override
    public void onStart() {
        super.onStart();
        UAirship.shared().getAnalytics();

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.findfriends);

        final EditText sbar = (EditText)findViewById(R.id.PlayerSeachBar);
        Button search = (Button)findViewById(R.id.Search);
        Button Add = (Button)findViewById(R.id.Add);
        final TextView ResultText = (TextView)findViewById(R.id.ResultTextView);
        final FrameLayout ResultFrame = (FrameLayout)findViewById(R.id.ResultFrameLayout);

        ResultFrame.setVisibility(View.GONE);

        search.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final String username = sbar.getText().toString();

                final ParseQuery query = ParseUser.getQuery();
                query.whereEqualTo("username", username);
                query.findInBackground(new FindCallback() {

                    @Override
                    public void done(List<ParseObject> objects, ParseException e) {
                        // TODO Auto-generated method stub
                        try {
                            ParseObject userObject = objects.get(0);
                            ResultText.setText(userObject.getString("username"));
                            ResultFrame.setVisibility(View.VISIBLE);

                            Toast.makeText(getApplicationContext(), "Player Found",
                                    Toast.LENGTH_LONG).show();
                        } catch (Exception e2) {
                            e2.printStackTrace();
                            Toast.makeText(getApplicationContext(), "Username Not Found",
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                });

            }
        });

        Add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String Friends = sbar.getText().toString();
                ParseUser currentUser = ParseUser.getCurrentUser();
                if (currentUser != null) {
                    {
                        currentUser.add("friend", Friends);
                        currentUser.saveInBackground();

                        Toast.makeText(getApplicationContext(), "Player Has Been Added",
                                Toast.LENGTH_LONG).show();

                    }
                }

            }
        });

    }

    @Override
    public void onStop() {
        super.onStop();

    }
}

PlayAFriend 类

package com.fullfrontalgames.numberfighter;

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.parse.ParseObject;
import com.parse.ParseUser;
import com.urbanairship.UAirship;

public class PlayAFriend extends ListActivity {
    private static final String TAG = null;

    Cursor fFriends;

    DBAdapter db;

    ParseObject objects;

    int from;

    @Override
    public void onStart() {
        super.onStart();
        UAirship.shared().getAnalytics();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playafriend);

        final ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser != null) {
            ArrayList<String> friendslist = new ArrayList<String>();
            final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1);
            ListView friendlv = (ListView)findViewById(android.R.id.list);
            friendlv.setAdapter(listAdapter);

            String friend = currentUser.get("friend").toString();
            listAdapter.add(friend);
        }
    }
}
4

3 回答 3

4

我使用文档中的 ParseRelation 方法自己解决了问题。这是我如何向当前用户添加朋友的示例!

FindFriends 类

public class Findfriends extends Activity {

    protected static final String TAG = null;

    ParseObject po;

    @Override
    public void onStart() {
        super.onStart();
        UAirship.shared().getAnalytics();

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.findfriends);

        final EditText sbar = (EditText)findViewById(R.id.PlayerSeachBar);
        Button search = (Button)findViewById(R.id.Search);
        Button Add = (Button)findViewById(R.id.Add);
        final TextView ResultText = (TextView)findViewById(R.id.ResultTextView);
        final FrameLayout ResultFrame = (FrameLayout)findViewById(R.id.ResultFrameLayout);

        ResultFrame.setVisibility(View.GONE);

        search.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final String username = sbar.getText().toString();

                final ParseQuery query = ParseUser.getQuery();
                query.whereEqualTo("username", username);
                query.findInBackground(new FindCallback() {

                    @Override
                    public void done(List<ParseObject> objects, ParseException e) {
                        // TODO Auto-generated method stub
                        try {
                            ParseObject userObject = objects.get(0);
                            ResultText.setText(userObject.getString("username"));
                            ResultFrame.setVisibility(View.VISIBLE);

                            Toast.makeText(getApplicationContext(), "Player Found",
                                    Toast.LENGTH_LONG).show();
                        } catch (Exception e2) {
                            e2.printStackTrace();
                            Toast.makeText(getApplicationContext(), "Username Not Found",
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                });

            }
        });

        Add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String Friends = sbar.getText().toString();
                final ParseUser currentUser = ParseUser.getCurrentUser();
                if (currentUser != null) {
                    {

                        final ParseObject friend = new ParseObject("Friends");
                        friend.put("username", Friends);

                        friend.saveInBackground(new SaveCallback() {

                            @Override
                            public void done(ParseException e) {
                                // TODO Auto-generated method stub
                                ParseRelation relation = currentUser.getRelation("Friends");
                                relation.add(friend);
                                currentUser.saveInBackground();
                            }

                        });

                        Toast.makeText(getApplicationContext(), "Player Has Been Added",
                                Toast.LENGTH_LONG).show();

                    }
                }

            }
        });

    }

    @Override
    public void onStop() {
        super.onStop();

    }
}

PlayAFriend 类

public class PlayAFriend extends ListActivity {
    private static final String TAG = null;



    ParseObject objects;



    @Override
    public void onStart() {
        super.onStart();
        UAirship.shared().getAnalytics();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playafriend);

        final ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser != null) {

            final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1);
            ListView friendlv = (ListView)findViewById(android.R.id.list);

            Button play = (Button)findViewById(android.R.id.button1);
            play.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }
            });
            friendlv.setAdapter(listAdapter);

            ParseRelation relation = currentUser.getRelation("Friends");

            ParseQuery query = relation.getQuery();

            query.whereEqualTo("username", null);

            query.findInBackground(new FindCallback() {

                @Override
                public void done(List<ParseObject> objects, ParseException e) {
                    // TODO Auto-generated method stub
                    for (int i = 0; i < objects.size(); i++) {
                        ParseObject r = objects.get(i);
                        String name = r.getString("username").toString();

                        listAdapter.add(name);
                    }

                }
            });
        }

    }

}
于 2013-06-22T06:26:11.623 回答
0

使用 Rest API 文档方法,基于解析器的回复,您可能希望将 array:friends 链接为用户类中的对象。

朋友对象包含一个指针数组,指向给定用户行中该用户的所有朋友的用户类中的 OID。

您可以通过在数组中添加或删除对象来随意更新指针数组。

'{"friends":{"__op":"Add","objects":[{"__type":"Pointer",
            "className":"User","objectId":"rtbhCb37tq"}]}}' 

当您想要获取给定 User ID 的所有朋友的完整子用户行时,只需将以下内容附加到 User 表中单行的普通查询中......

--data-urlencode 'include=friends'

解析 RestAPI Docs 有一些涉及游戏、GameScores 和 GameOpponents 的这种技术的好例子。你可以读那个。

如果您想评估最高级的客户端,我将从“ okhttp ”和“ volley ”开始。

这里这里更传统的 http 东西

于 2013-06-09T15:10:11.853 回答
0

我已经查看了您的答案,我相信您可以稍微改进一下代码。

我发现您创建了一个 ParseObject 朋友并为它提供了使用 检索的属性“用户名” String Friends = sbar.getText().toString();,然后继续创建新创建的 ParseObject 和当前 ParseUser (currentUser) 之间的关系。

我发现的问题在 Parse DB Storage 中。如果您查看您的 Parse DB,您会发现您正在创建的 ParseObject 朋友与您最初查询的 ParseUser 不共享相同的 ObjectID 或任何属性/数据。

您基本上制作了一个 ParseObject,它只不过是 ParseUser 用户名的副本。我还发现您可以多次添加相同的 ParseObject 朋友,因为每次这样做时,您都会创建一个具有单独 ObjectID 的新 ParseObject,从而引用一个完全不同的 ParseObject。

理想情况下,您会希望直接引用 ParseUser ,我相信我已经知道如何做到这一点。

这是我定义的整个适配器,但它允许您直接创建与 ParseUser 的关系,而不必创建 ParseObject

public UserQueryAdapter(Context context, final String searchCriteria) {
        // Use the QueryFactory to construct a PQA that will only show
        // Todos marked as high-pri

        super(context, new ParseQueryAdapter.QueryFactory<ParseUser>() {
            public ParseQuery<ParseUser> create() {
                ParseQuery<ParseUser> query = ParseUser.getQuery();
                query.whereContains("username", searchCriteria);
                return query;

            }
        });
    }

    @Override
    public View getItemView(final ParseUser pUser, View v, ViewGroup parent) {

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

        super.getItemView(pUser, v, parent);

        ParseImageView todoImage = (ParseImageView) v
                .findViewById(R.id.imageViewSearch);
        ParseFile imageFile = pUser.getParseFile("photo");
        if (imageFile != null) {
            todoImage.setParseFile(imageFile);
            todoImage.loadInBackground();
        }

        // Add the title view
        final TextView titleTextView = (TextView) v
                .findViewById(R.id.textViewSearch);
        titleTextView.setText(pUser.getUsername());

        btnAdd = (ImageButton) v.findViewById(R.id.imageButtonAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Drawable background = v.getResources().getDrawable(
                        R.drawable.ui_border_green);
                Drawable img = v.getResources().getDrawable(
                        R.drawable.ico_friend_add_green);

                btnAdd.setBackground(background);
                btnAdd.setImageDrawable(img);

                final ParseUser currentUser = ParseUser.getCurrentUser();

                if (currentUser != null) {
                    {


                        ParseRelation<ParseUser> relation = currentUser
                                .getRelation("Friends");
                        relation.add(pUser);
                        currentUser.saveInBackground();

                    }
                }

            }
        });

        return v;

    }
于 2014-08-18T03:49:15.293 回答