Passing a parse query to another activity? - android

ParseRelation<ParseObject> p = country.getRelation("view");
ParseQuery P2 = p.getQuery();
So is it possible to intent the P2 query in the next activity ,so that we can use this query in next activity to fetch the data from parse database.

Sadly ParseObject is not parcelable, which would solve hundreds of issues I had with their Android SDK.
But you can surely pass country through its object id.
// Launching...
Intent i = new Intent(context, Destination.class);
i.putExtra(“relationObjectId”, country.getObjectId());
i.putExtra(“relationFieldName”, “view”);
startActivity(i);
// Then in Destination activity..
Intent i = getIntent();
String relationObjectId = i.getStringExtra(“relationObjectId”);
String relationFieldName = i.getStringExtra(“relationFieldName”);
Country country = ParseObject.createWithoutData(Country.class, relationObjectId);
ParseQuery query = country.getRelation(relationFieldName);
Please note that before calling getRelation() again, you might have to use fetchIfNeededInBackground().

Related

intent.putExtra passing object android studio+ firebase

i'm doing a simple crud android project
i'm absolute beginner
so i watched a tuto on youtub
i found that he passed un object to intent.putExtra
like this
Employee emp = e==null? list.get(position):e;
case R.id.menu_edit:
Intent intent=new Intent(context,MainActivity.class);
intent.putExtra("EDIT",emp);
context.startActivity(intent);
break;
code source from here https://github.com/Sovary/FireTestRecyclerView/blob/main/app/src/main/java/com/hellokh/sovary/firetest/RVAdapter.java#L56
and it dosn't work with me !
intent.putExtra can't hold an object in it in my code it only support string or something
what to do to pass this argument ?
i tried to change it to char or to give one attribut but no hope hh
library:
implementation 'com.google.code.gson:gson:2.10'
Convert your object data to string and send your data as string through intent:
case R.id.menu_edit:
Intent intent = new Intent(context,MainActivity.class);
Gson gson = new Gson();
//transform a java object to json
String employee_string = gson.toJson(Employee.class);
intent.putExtra("EDIT",employee_string);
context.startActivity(intent);
break;
On the destination page get convert the string to object through gson and retrive your data:
String data = getIntent().getStringExtra("EDIT");
//Transform a json to java object
Employee employee= gson.fromJson(json_ data, Employee.class); // here is your data as object

What arguments can I include with a map marker

I'm creating an map that shows a result from a Parse query. I can pass information directly into the marker, such as a title or a lat/long, but I would like to include some additional information (not to be displayed) that I can use when the user clicks on the marker.
For instance when the user clicks on the marker, I would like to find the user's objectId from the Parse query (which is simply a string).
I've tried the following code in the marker onClickListener to put the string in as an argument:
destPosition = marker.getPosition();
destLat = destPosition.latitude;
destLong = destPosition.longitude;
MarkerDialogFragment markerDialogFragment = new MarkerDialogFragment();
Bundle args = new Bundle();
args.putString("id", marker.getId());
args.putString("userId", userId);
args.putString("title", marker.getTitle());
args.putDouble("latitude", destLat);
args.putDouble("longitude", destLong);
markerDialogFragment.setArguments(args);
when I try to extract this data on the "other side" by getting the arguments, the userId always comes across as null...I've defined userId as such:
final String userId = parseUsers.get(i).getObjectId();
I've even tried hard coding the string in userId as such, and it still comes back as null when I try to get the arguments:
final String userId = "test userId";
Here is the get arguments code for the marker onCreateDialog method:
builder = new AlertDialog.Builder(getActivity());
final String markerId = getArguments().getString("id");
final String userId = getArguments().getString("userId");
final String title = getArguments().getString("title");
final double destLat = getArguments().getDouble("latitude");
final double destLong = getArguments().getDouble("longitude");
builder.setTitle(title)
When I use the debugger to stop the code to see the values, all data is set as expected and I have values for each item EXCEPT the userId. Can anyone help point me in the right direction? I simply want to pass a value associated with a variable and have it be tied to each marker created on the map. Thanks!
You cannot add anymore data to the marker.
So if you want to add more data to your marker, you will have to store it in an external variable with marker's ID (getID())
eg -
HashMap<int,Object> extramarkerData = new HashMap<int,Object>();
and put data into hashmap
extramarkerData.put(marker.getId(),"additional data");

how to send a string to listView in another activity ? in android

Good evening guys,
I'm making an app and I want to know how to send a string to "List-View" in another activity ?
You can send data using the following code -
Intent intent = new Intent(this,newActivity);
intent.putExtra(name, value)
name = The name of the extra data
value = The String data value.
startActivity(intent);
In the new activity, you receive string via following (in onCreate)
Intent intent = getIntent();
String str = intent.getString(name)
name = The name of the extra data
Now search the web on how to add a string to list view. You will find it easily

Anyway to have more than one extra added to intent?

I want to have more than one extra added to Intent. One to hold a double and one to hold long. Is this possible?
If so, how would I do it and how would I get the information from each extra?
You can add as many extras to an Intent as your heart desires, they are all just key value data:
Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);
And they can be retrieved using the same key names:
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
intent.putExtra(#ExtraDoubleKey, #ExtraDoubleValue);
intent.putExtra(#ExtraLongKey, #ExtraLongValue);
Where #ExtraDoubleKey is a string that you will use to access the extra (i.e. "price" or something), and #ExtraDoubleValue is the value of the extra (the double variable you wish to pass). Similarly for #ExtraLongKey and #ExtraLongValue.
Then to access the extras in your next activity you can use:
double doubleValue = getIntent().getExtras().getDouble(#ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(#ExtraLongKey);
to get the value of the double extra with the key #ExtraDoubleKey.
https://stackoverflow.com/a/11461530/776075
Devunwired is correct.
But the way i see is
you can keep only one value per Type.
Like one string, one int, one double etc..
You are not capable of containing 2 string values. or two integers.
I have experienced this on a program and i have overcome it by using
one string and one Boolean.
You can use Bundle and pass it as a parameter to the Intent.
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent

Sending LinkedHashMap to intent

I want send LinkedHashMap to another Intent. But I don't known what method for extras is allowable.
Bundle extras = getIntent().getExtras();
LinkedHashMap<Integer, String[]> listItems = extras.get(LIST_TXT);
You cannot reliably send a LinkedHashMap as an Intent extra. When you call putExtra() with a LinkedHashMap, Android sees that the object implements the Map interface, so it serializes the name/value pairs into the extras Bundle in the Intent. When you want to extract it on the other side, what you get is a HashMap, not a LinkedHashMap. Unfortunately, this HashMap that you get has lost the ordering that was the reason you wanted to use a LinkedHashMap in the first place.
The only reliable way to do this is to convert the LinkedHashMap to an ordered array, put the array into the Intent, extract the array from the Intent on the receiving end, and then recreate the LinkedHashMap.
See my answer to this question for more gory details.
The best way is to convert it to ArrayList of the same type examble :
LinkedHashSet<Long> uniqueIds = new LinkedHashSet();
ArrayList<Long> ids = new ArrayList(uniqueIds);
Intent intent = new Intent(getContext(), UserStoryActivity.class);
intent.putParcelableArrayListExtra(FLAG_USERS_STORY_LIST_IDS, (ArrayList) ids);
startActivity(intent);
Thanks #David Wasser for pointing out this issue.
I have another approach is to use Gson to parse your whatever map to json string, then put into the intent. Less drama!
Code send:
gson.toJson(data)
Code get back:
Kotlin:
gson.fromJson<LinkedHashMap<Int, Int>>(
it.getStringExtra(ARG_DATA),
object : TypeToken<LinkedHashMap<Int, Int>>() {}.type
)
Java:
gson.fromJson(
it.getStringExtra(ARG_DATA),
new TypeToken<ArrayList<Mountain>>(){}.getType()
);

Categories

Resources