在我的应用程序中,我试图创建一个与 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);
}
}
}