How do you query from Parse between two different activities in Android? - java

so I store some basic data using parse in one activity, but how do I retrieve that data from parse(query it) in another activity? Can someone give me a clean cut example?
So in my main activity I have
public String max = "max";
Parse.enableLocalDatastore(this);
private ParseObject rightCardsStore = new ParseObject("RightCardsStore");
rightCardsStore.put("max",max);
rightCardsStore.saveInBackground();
Now, in another activity, "Folder.java" I want to query/retrieve that data and use that string.

As hitch.united said, you need to get the ID by doing a saveInBackground and send it to the other activity:
rightCardsStore.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Intent intent = new Intent(YourCurrentActivity.this, YourNewActivity.class);
intent.putExtra("parseObjectId", rightCardsStore.getObjectId());
YourCurrentActivity.this.startActivity(intent);
}
}
});
Then you can retrieve the data in your other activity, and query parse:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String id = bundle.getString("parseObjectId");
ParseQuery<ParseObject> query = ParseQuery.getQuery("RightCardsStore");
query.getInBackground(id, new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object is the RightCardsStore you just saved
String max = object.getString("max");
}
}
}
}
If you just need to use max as a readonly value you can simplify the process by replacing (in the first activity)
intent.putExtra("parseObjectId", rightCardsStore.getObjectId());
by
intent.putExtra("max", max);
and replacing the second activity by:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String max = bundle.getString("max");
}

After the saveInBackground get the ID of the object and pass it with an intent to the new activity. You can then requery with that ID to retrieve the object.

Related

How do I retrieve info of other users in Parse in Android?

ive been trying for a long time and i cant manage to find a solution
in the User class in parse, I added a new column and called it bio, im trying to retrieve that so i can set it inside the user's profile TextView, inside of android studio ofc
Intent intent = getIntent();
String usersUsername = intent.getStringExtra("username");
ParseQuery<ParseUser> bioQuery = ParseQuery.getQuery("User");
bioQuery.whereEqualTo("username", usersUsername);
bioQuery.getInBackground("bio", new GetCallback<ParseUser>() {
#Override
public void done(ParseUser object, ParseException e) {
if (e == null){
Log.i("info", "happy");
} else {
Log.i("info", "sad");
}
}
});
im getting the right username with the intent, im getting it from a listview that hold the users usernames
i always get "sad", i tried multiple other ways and regarding those other ways it
kept on telling me that there were no queries that match
please assist me, thank you.
Try with findInBackground. It should be something like:
Intent intent = getIntent();
String usersUsername = intent.getStringExtra("username");
ParseQuery<ParseUser> bioQuery = ParseQuery.getQuery(ParseUser);
bioQuery.whereEqualTo("username", usersUsername);
bioQuery.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
if (e == null){
Log.i("info", "happy");
} else {
Log.i("info", "sad");
}
}
});
You also need to make sure that you have access to the user. If it is not the current logged in user, you probably do not have access.
for anyone looking for an answer to a question like mine, I managed to get a solution, which at second glance should've been obvious
I took Davi's advice and added to it what I needed
Intent intent = getIntent();
String usersUsername = intent.getStringExtra("username");
ParseQuery<ParseUser> bioQuery = ParseUser.getQuery();
bioQuery.whereEqualTo("username", usersUsername);
bioQuery.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
if (e == null){
Log.i("info", "happy");
for(ParseUser user : objects){
Log.i("info", (String) user.get("bio"));
bio = (String) user.get("bio");
}
fullBioTextView.setText(bio);
} else {
Log.i("info", "sad");
}
}
});

How to pass data from a fragment class to an activity

Please, I am having problem sending an ArrayList data to Activity.
The data is an URL saved in ArrayList but after using bundle extras, I am getting null pointer exception. I also try to catch the exception but still the data i get using get extra string is null
// below is the code i used to pass the data
Bundle extras = new Bundle();
extras.putString("VidUrl",VideoLecturesUrl.get(position));
extras.putString("bookUrl",bookUrl.get(position));
extras.putString("VidTitle",titleList.get(position));
Intent intent = new Intent(getActivity(),DetailsView.class);
intent.putExtras(extras);
startActivity(intent);
// below is the receiving activity
Intent intent = getIntent();
extras =intent.getExtras();
if (extras!=null){
try {
Video_Url = extras.getString("VidUrl");
BookUrl = extras.getString("bookUrl");
Title = extras.getString("VidTitle");
Toast.makeText(this,Video_Url,Toast.LENGTH_SHORT).show();
Toast.makeText(this,BookUrl,Toast.LENGTH_SHORT).show();
Toast.makeText(this,Title,Toast.LENGTH_SHORT).show();
videotexTitle.setText(Title);
setTitle(Title);
setVideo(Video_Url.toString());
}catch (Exception e){
e.printStackTrace();
}
} else {
Toast.makeText(this, "the extrass is empty", Toast.LENGTH_SHORT).show();
}
in the fragment
public interface NoticeFragmentListener {
public void onFragmentSendArray(ArrayType yourArray);
}
NoticeFragmentListener listener;
#Override
public void onAttach(Context context) {
super.onAttach(context);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeFragmentListener so we can send events to the host
listener = (NoticeFragmentListener) context;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
}
}
//metod to send array
public void SendArray(ArrayType yourArray){listener.onFragmentSendArray(yourArray);}
in de activity
public class yourActivity extends AppCompatActivity implements YourFragment.NoticeFragmentListener
and recive the array
#Override
public onFragmentSendArray(ArrayType yourArray) {
}
For simplicity use the SharedPreferences for this purpose

Getting Null Values while passing data with intent from adapter to activity

This is probably a very basic question but I couldn't find the answer.
I need to pass data from RecyclerView but when I check it on related activity, data is null.
mvHolder.barcode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent s = new Intent(context, SummaryActivity.class);
s.putExtra("SUMMARY",sp.getDATA().get(i).getId_summary()); // data is not null
context.startActivity(s);
}
});
and this is how I try to achieve it :
Bundle extras = getIntent().getExtras();
if (extras != null) {
strBarcode = extras.getString("SUMMARY");
// and get whatever type user account id is
}
but when I debug it, it turns out that strBarcode is null. I don't know what the problem is, I guess my code should be works either. Please help me
Intent data = getIntent();
if (data != null) {
strBarcode = data.getStringExtra("SUMMARY");
// and get whatever type user account id is
}
Intent data = getIntent();
if (data != null) {
int strBarcode = data.getIntExtra("SUMMARY");
}
Please try this
Check if getIntent().hasExtra("SUMMARY") returns true.
If it does, make sure that sp.getDATA().get(i).getId_summary() returns a String.
The extra might be there, but you try to get as String, and it might
have different type.

Passing bundle instance to place picker intent

I am working on an android app whose main purpose is to update the working location of the employees by admin. Now when I want to change/update the location of an employee from my recycler view(list of employees connected with my UserManagerAdapter), I have to pass the user name of that employee to the place picker intent so that when the admin pick the desired location, the database of that user will be changed accordingly.
My Steps(2 Steps)
I have passed the username to the place picker intent as bundle.
My UserManagerAdapter
holder.locationTv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
launchPicker(data.get(position).getUserName());
}
});
private void launchPicker(String userName) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
Bundle bundle = new Bundle();
bundle.putString(USERNAME,userName);
try {
fragment.startActivityForResult(builder.build(fragment.getActivity()),PLACE_PICKER_REQUEST,bundle);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
I received the location request inside of a fragment and update the location of that particular user
My ManageUserFragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PLACE_PICKER_REQUEST){
if(resultCode == RESULT_OK){
Place place = PlacePicker.getPlace(getContext(),data);
address = place.getAddress().toString();
String latLng = place.getLatLng().toString();
latLang = latLng;
//update user's decided location
Bundle bundle = data.getExtras();
String userName = bundle.getString(USERNAME);// it returns null, Why?
updateLocation(latLang,userName);
Toast.makeText(getContext(), latLng, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getContext(), getContext().getText(R.string.locationError), Toast.LENGTH_SHORT).show();
}
}
}
My constant is
public static final String USERNAME="Username";
Now,
My problem is
Why bundle.getString(USERNAME) always return null?
How to pass data to place picker intent so that we can receive it in
onActivityResult ?
After replicating your case and researching for a little bit, I found out that the third parameter in startActivityForResult() is not used to pass a bundle of values to the onActivityResult, it's used to pass options to Android itself, you can find those here. So if you want to pass any data you have to use an intent with putExtra("USERNAME", username), and you can retrieve it with getStringExtra("USERNAME"). It's explained in this answer as well.

getIntent().getSerializableExtra is null

I have a POJO class implements Serializable
and I want to transfer an object of this class to another activity with a help Intent and Bundle.
I checked the object before the transfer, it is not null. (Correctly get one of attributes)
put:
private void onFriendClick(FriendHolder holder, int position) {
Intent intent = new Intent(context, ProfileActivity.class);
Bundle extra = new Bundle();
extra.putSerializable(Consts.KEY_USER_JSON, friendList.get(position));
intent.putExtra(Consts.KEY_USER_JSON, extra);
Log.e("onFriendClick", String.valueOf(friendList.get(position).getName()));
context.startActivity(intent);
}
get from another activity:
private void setupProfile() {
Bundle extras = getIntent().getExtras();
if (extras != null) {
profile = (ProfileDTO) getIntent().getSerializableExtra(Consts.KEY_USER_JSON);
Log.e("onFriendClick", String.valueOf(profile.getName()));//NPE this
} else profile = user.getProfile();
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.String
ru.techmas.getmeet.api.models.ProfileDTO.getName()' on a null object
reference
at ru.techmas.getmeet.activities.ProfileActivity.setupJsonProfile(ProfileActivity.java:103)
You don't need to create a Bundle.
Try doing:
private void onFriendClick(FriendHolder holder, int position) {
Intent intent = new Intent(context, ProfileActivity.class);
intent.putExtra(Consts.KEY_USER_JSON, friendList.get(position));
Log.e("onFriendClick", String.valueOf(friendList.get(position).getName()));
context.startActivity(intent);
}
private void setupProfile() {
if (getIntent().getSerializableExtra(Consts.KEY_USER_JSON) != null) {
profile = (ProfileDTO) getIntent().getSerializableExtra(Consts.KEY_USER_JSON);
Log.e("onFriendClick", String.valueOf(profile.getName()));//NPE this
} else {
profile = user.getProfile();
}
}
But if still want to use the Bundle, you should replace in the code that you posted:
profile = (ProfileDTO) getIntent().getSerializableExtra(Consts.KEY_USER_JSON);
By:
profile = (ProfileDTO) extras.getSerializableExtra(Consts.KEY_USER_JSON);

Categories

Resources