在我的申请中,我试图建立一个与CurrentUser相关的朋友列表,我已经尝试查看Parse.com的文档,并在Parse上询问了有关此问题的问题。 com和一个Parser建议我以Array列的形式进行。虽然它似乎与CurrentUser相关(我做了3个帐户并使它们成为朋友,并且3个帐户具有不同的数组列,但我已经这样做了)从ListView中检索出来)这不是我想要的,因为它只是帐户的用户名而不是他们的个人"行"如果你知道我的意思,就可以对用户采取行动。
所以我问的问题是, 使用Parse后端数据库制作用户友情列表"的最佳做法是什么?基于CurrentUser方法?
以下是我的2个活动,其中FindFriends是我搜索用户并将其添加到" friendlist"以及用户通过AdapterArray列表视图加载的PlayAFriend。
FindFriends Class
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 Class
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);
}
}
}
答案 0 :(得分:4)
我使用docs中的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 Class
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);
}
}
});
}
}
}
答案 1 :(得分:0)
使用Rest API docs方法,基于解析器的回复, 您可能希望链接数组:朋友作为用户类中的对象。
friends对象包含指向给定用户行中该用户的所有朋友的User类中OID的指针数组。
您可以通过在阵列中添加或删除对象来随意更新指针数组。
'{"friends":{"__op":"Add","objects":[{"__type":"Pointer",
"className":"User","objectId":"rtbhCb37tq"}]}}'
如果要获取给定用户ID的所有朋友的完整子用户行, 您只需将以下内容附加到User表中的单行的常规查询...
--data-urlencode 'include=friends'
解析RestAPI Docs有一些很好的例子,包括Games,GameScores和GameOpponents。你可以读到它。
答案 2 :(得分:0)
我已经回复了您的回答,我相信您可能会稍微改进一下代码。
我发现您创建了一个ParseObject好友并为其提供了属性&#34; username&#34;使用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;
}