How do I know what data is given in a Bundle? - android

I'm having a heck of a time figuring out what data is coming to my methods through Intent/Bundles. I've tried adding break points to inspect the data, but I don't see anything. Perhaps because it's a Parcelable I can't manually read it in Eclipse.
For instance, a onActivityResult(int requestCode, int resultCode, Intent data) for a Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI). How do I know what data is available? Notice, I'm not ask for WHAT data is available but how the heck do I figure it out so I can apply the same idea to any Bundle/Intent from the Android framework? Perhaps it's a simple as looking at the docs, but I don't see a full listing of the data and I can't see it in Eclipse. So I'm at a lost.

Bundle.keySet() gives you a list of all keys in the bundle. That said, typically you just expect certain keys and query them, but keySet() is used to examine bundles you get from somewhere.

public static String bundle2string(Bundle bundle) {
if (bundle == null) {
return null;
}
String string = "Bundle{";
for (String key : bundle.keySet()) {
string += " " + key + " => " + bundle.get(key) + ";";
}
string += " }Bundle";
return string;
}

i getting alll key and value of bundle stored...
for (String key : bundle.keySet()) {
string += " " + key + " => " + bundle.get(key) + ";";
}
output:
(key) :(value)
profile_name:abc

The only thing you get out of a Bundle is what you put in. Bundles are ways of passing information between activities. If you're in charge of your entire application, you shouldn't need to look inside the Bundle for your objects, you should just grab them. Think hashmap keys... if you don't know the key, it's not like you can search the hashmap.
To place an item into a Bundle and pass it on to the next activity, you need to put it as an Extra. Take a look here for an example of passing data via extras and bundles between activities.
Copied and pasted below:
From Activity1
Intent intent = new Intent(this,myActivity2.class);
Bundle bundle = new Bundle();
bundle.putString("myValue", myValue);
intent.putExtras(bundle);
navigation.this.startActivity(intent);
In Activity2
Bundle bundle = getIntent().getExtras();
act2MyValue= bundle.getString("myValue");

Related

Passing data from listview

Good night, to pass data from one listview generated a database I do so by Intent and capturing the value:
intent.putExtra("name", listView.getItemAtPosition(position).toString());
To pick do with Bundle:
Bundle bundle = getIntent().getExtras();
String myData = bundle.getString("name");
nombre.setText(miData);
The problem is that I receive is in the form of array:
{value = first name}
I tried various shape and not get capture only the string "first course" because it is what is shown in the listview, any suggestions? Thank you!
PD -> Sorry if my English is not good.
Thanks for the reply but I do not get what I want, when I pick up the data from the database and pick them up
arrayList.add (createList ("value" name));
With the index or value
But I have solved:
int start = myData.indexOf("=");
int end = myData.indexOf("}", start + 1);
nombre.setText(myData.substring(start + 1, end));
is not an elegant solution, but eventually get him in the EditText name.

Passing data between activities in Android?

I wish to pass the value of score from one activity to another. I addExtras to the intent and getExtras in the new activity but it doesn't seem to get the value.
Activity 1;
Intent intent = new Intent(Game.this, EndGame.class);
intent.putExtra("put_score", score);
startActivity(intent);
Game.this.finish();
Activity 2;
Bundle extras = getIntent().getExtras();
if (extras != null) {
score = extras.getString("put_score");
}
setContentView(R.layout.endgame);
scoreResult = (TextView) findViewById(R.id.scoreNum);
scoreResult.setText(score);
You problem is coming from the following piece of code in Bundle.java:
try {
return (String) o;
} catch (ClassCastException e) {
typeWarning(key, o, "String", e);
return null;
}
Here o is the object you put to the bundle (bundle actually has a core storage of type Map<String, Object>, so, due to autoboxing, when you put int to the bundle, it will become Integer). But, unfortunately, Integer cannot be casted to String explicitly, so you get null instead. In other words: if you put int, then use getInt to retrieve the value.
you placed data in intent using putExtra not putExtras
so read them the same way
use
getXXExtra()
XX is the dataType your data is,
based on the example, if score is Integer, then use:
getIntExtra("put_score", 0);//0 zero is default in case data was not found
http://developer.android.com/reference/android/content/Intent.html

get the value from Intent of android

Thanks in advance.
When I print Log.d("me",getIntent().toString());
I am getting:
Intent { act=android.intent.action.CALL_PRIVILEGED dat=tel:888 flg=0x13800000 cmp=com.ninetology.freecall.two/.CallFinalActivity }
I am trying to fetch the value which is associated with "dat" but I am getting NullPointer exception.
//the code I am using is
getIntent().getStringExtra("dat"); // no use
//i tried
getIntent().getExtras("dat").toString(); // NullPointer exception
I tried with "tel" as key in above code still no use.
it seems you're doing this wrong.
The getExtras() function returns a bundle that you can extract data from and not a function that returns a specific String.
dat is NOT a String value as you can see from the data that was printed. it's a Uri,
try parsing it as you should and I'm sure you'll be able to get the data.
public void onCreate(Bundle b) { //mistyped
super.onCreate(b);
Uri data = getIntent().getData();
// OR USE THIS
String data = getIntent().getDataString();
// DO STUFF
}
try getIntent().getExtras().get("dat");
First of all, Its not necessary the string from Intent your are getting in log have a object with values..
So its better to just check its not a null, like,
Bundle bundle = getIntent().getExtras();
if(bundle ! = null)
{
// Now check you bundle object which has a values or not
}
else
{
// 1. get data in form of Uri
Uri data = getIntent().getData();
// 2. OR get string of Uri
String dataString = getIntent().getDataString();
// 3. Or split the data string
// The logic from this part may be different on your requirement.. I only suggests you to get data from string.. (Actual logic may different on your case)
String data = getIntent().toString();
data = data.subString(data.indexOf(":"), data.indexOf("flg")-1);
Log.e("tel:", data);
}
When you want to pass the data with the intent just add the below code before starting activity
intent.putExtra("dat", value); //value=the value you want to send
And when you want to fetch the same value in another activity just do:
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
String string=bundle.getString("dat");
}
By doing this, you wont get the null pointer exception and will help you.

Order of items in the keySet of Bundle

Is the order of items in the .keySet() of a Bundle in the same order as items were inserted? I am trying to send a LinkedHashMap through a parcelable from a service to the activity.
Here is the code to insert a linkedHashMap's items into a Bundle using .putSerializable:
Bundle b = new Bundle();
Iterator<Entry<Integer, String>> _cyclesIterator = cycles.entrySet().iterator();
while (_cyclesIterator.hasNext()) {
Entry<Integer, String> _entry = _cyclesIterator.next();
Log.i(TAG,"Writing to JSON CycleID" + _entry.getKey());
b.putSerializable(_entry.getKey().toString(), _entry.getValue());
}
_dest.writeBundle(b);
I am trying to read this back when the bundle is retrieved using this:
Bundle _b = in.readBundle();
if (_b != null) {
for (String _key : _b.keySet()) {
Log.i(TAG,"Reading JSON for cycle " + _key);
_lhm.put(Integer.valueOf(_key), (String)_b.getString(_key));
}
setCycles(_lhm);
}
When items go in, the log says [1,2,3,4] but when they're read back, it is [3,2,1,4]. Is there a way to ensure the same order in both?
Maps have different approach according to their use.
HashMap - The elements will be in an unpredictable, "chaotic", order.
LinkedHashMap - The elements will be ordered by entry order or last reference order, depending on the type of LinkedHashMap.
TreeMap - The elements are ordered by key.
For getting better idea you can also check this Answer.
Maps and sets (and Bundles) carry no inherent order. You could serialize a single TreeMap<Integer, String> instead of iterating and serializing each entry, or you could iterate like you're doing but then also store one more value -- an ArrayList (also serializable) of the keys.

NullPointerException from getIntent().getExtras().getString("to")

InboxDetailActivity.java:
Intent i = new Intent(InboxDetailActivity.this,Compose.class);
Bundle b = new Bundle();
b.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
b.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
b.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
i.putExtras(b);
startActivity(i);
Compose.java:
Intent i = getIntent();
Bundle b = i.getExtras();
to = b.getString("To");
subject = b.getString("Subject");
toId = b.getString("FromId");
I am getting NullPointerException at to = b.getString("To");
Bundle b = i.getExtras();
getExtras() returns null.
Agree with John's answer adding possible solution.
What you are doing is create a bundle , insert values in this and then pass this bundle.
And than you just fetch all the values one by one using its keys.
I am working with bundles but I simply add desired values directly using putExtra method. And I haven't got any problem till date. I recommend you to use put extra and check whether it works or not.
I would like to know what makes you to apply this way for bundles? Have you just read it somewhere and started applying this method ? or you have got some options and after some consideration you found it better to apply this method OR your requirement states that. Because normally me and my peers doesn't use bundles and directly pass the extras. And that works for me every time.
using this instead of bundle
i.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
i.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
i.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
and in another class..
to = getIntent().getString("To");

Categories

Resources