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
Related
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().
I want to get parameters from this response !!
Bundle[{custom={"custom data":{"notification_type":"offer","offer_id":4348}}, from=1013970362419, badge=1, message=birds view, android.support.content.wakelockid=4, collapse_key=do_not_collapse}]
I want offer_id and notification_id , in above notification data
As the response is received in Bundle, you cannot parse it directly. Instead you can get each parameter of bundle using bundle.getString(key) or bundle.getInt(key) whatever type of data is.
So in your response to get offer_id, first of all extract custom parameter as a string and convert it into JSONObject. Then you will be able to get offer_id.
E.g.
JSONObject keyData = new JSONObject(data.getString("custom"));
Gson gson = new Gson();
KeyValueModel keyValue= gson.fromJson(keyData.toString(), KeyValueModel .class);
String offerId = keyValue.getString("offer_id")
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
I am creating one application,in my application I am using json parsing,I am sending request to server for
after click on button request will be send,and i will get response
{"searchresult":
[ {"match_detail_id":369,"profile_id":"GM686317","name":"Sonal Patel","image":"","location":"Rajkot ,Gujarat ,India","mothertongue":"Gujarati","religion":"Hindu","occupation":"Professor \/ Lecturer","education":"Masters - Arts\/ Science\/ Commerce\/ Others"}
, {"match_detail_id":396,"profile_id":"GM780609","name":"Hetal Trivedi","image":"","location":"Rajkot ,Gujarat ,India","mothertongue":"Gujarati","religion":"Hindu","occupation":"Administrative Professional","education":"Bachelors - Arts\/ Science\/ Commerce\/ Others"}
, {"match_detail_id":1078,"profile_id":"GM540027","name":"Shruti Dave","image":"","location":"Rajkot ,Gujarat ,India","mothertongue":"Gujarati","religion":"Hindu","occupation":"Education Professional","education":"Masters - Arts\/ Science\/ Commerce\/ Others"}
]
}
I want to show this response in another activity
Parse the response you have received, and store it in a arrayList with the custom object you create to store your each response line.
{"match_detail_id":369,"profile_id":"GM686317","name":"Sonal Patel","image":"","location":"Rajkot ,Gujarat ,India","mothertongue":"Gujarati","religion":"Hindu","occupation":"Professor \/ Lecturer","education":"Masters - Arts\/ Science\/ Commerce\/ Others"}
this is your one line, so object class would be like this -
public class ResultObejct implements parcelable {
String match_detail_id;
String profile_id;
String name;
String image;
public String getName(){ //set getter and setters
return this.name;
...
...
...
}
And Like this link, put your data in intent,
Intent i = new Intent(...);
i.putExtra("data", new DataWrapper(yourArrayList));
and retrieving in next activity:
DataWrapper dw = (DataWrapper) getIntent().getSerializableExtra("data");
ArrayList<Parliament> list = dw.getParliaments();
Store the response data in a map and pass it to next activity.
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()
);