Reputation: 1183
In my application I'm trying to make a friendlist that's relative to the CurrentUser,I've tried looking through the docs of Parse.com and I asked a question about this on Parse.com and a Parser suggested I do it in the form of a Array column.I've done this although it seems to be relative to the CurrentUser(I made 3 accounts and made them friend eachother and the 3 accounts have different array columns retrieved from the ListView)It's not what I'm looking for since it's just the Usernames of the accounts and not their individual "rows" to make actions on the Users if you know what I mean.
So the question I'm asking is, What would be the best practice using the Parse backend database to make a User "friendlist" based on the CurrentUser method?
Here is my 2 activities in which FindFriends is where I search for the Users and Add them to the "friendlist" and the PlayAFriend in which the friended Users would load up via an AdapterArray listview.
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);
}
}
}
Upvotes: 2
Views: 5321
Reputation: 1858
I have reviewed your answer and I believe you may be able to improve the code a bit.
I found that you create a ParseObject friend and provide it with the attribute "username" which is retrieved with String Friends = sbar.getText().toString();
, and then proceed to create a relation between that newly created ParseObject and the current ParseUser (currentUser).
The problem I found is within the Parse DB Storage. If you review your Parse DB you will find that the ParseObject friend that you are creating does not share the same ObjectID, or any of the attributes/data as the ParseUser that you are initially querying for.
You basically make a ParseObject that is no more than a copy of a ParseUser's username. I also found that you can add the same ParseObject friend multiply times, because everytime you do so you create a new ParseObject with a separate ObjectID and thus referencing a totally different ParseObject.
Ideally you would be looking to reference the ParseUser directly and I believe I have figured out how to do so.
Here is the entire Adapter that I have defined, but it allows you to create a relation with a ParseUser directly rather than having to create a 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;
}
Upvotes: 0
Reputation: 1183
I solved question myself using the ParseRelation method from the docs.Here is my example on how to add a friend to the current user!
FindFriends class
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);
}
}
});
}
}
}
Upvotes: 4
Reputation: 6289
useing the Rest API docs approach, building on the reply you have from a parser, you may want to link an array:friends as an object in your user class.
The friends object contains an array of pointers to the OID's in User class of all the friends of that user in the given user row.
You can update the array of pointers as you please by adding or removing objects from the array.
'{"friends":{"__op":"Add","objects":[{"__type":"Pointer",
"className":"User","objectId":"rtbhCb37tq"}]}}'
When you want to get the full , child user rows of all the friends of a given User ID , you just append the following to a normal query for a single row from the User table...
--data-urlencode 'include=friends'
The parse RestAPI Docs has some good examples of this technique involving Games, GameScores, and GameOpponents. You could read that.
If you want to eval the most advanced clients i would start with 'okhttp' and 'volley'.
More traditional http stuff here and here
Upvotes: 0