Bundle.putInt() , how does it work? - android

I am still learning android and I in a tutorial
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
My problem is here I didn't understand how this works
dataBundle.putInt("id", id_To_Search);
if id_To_Search = 0 then id = 0 ? and id refer to the column name ?
android site explains
putInt(String key, int value)
Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key.
as far I understand, if int = 5 then key will be 5 ?
then 'Id' it will be = 5 ?

1) You can share int with other activity like this :
String YOUR_KEY = "id";
Intent intent = new Intent(getApplicationContext(), DisplayContact.class);
intent.putExtra(YOUR_KEY, id);
startActivity(intent);
or
Bundle dataBundle = new Bundle();
dataBundle.putInt(YOUR_KEY, id);
intent.putExtras(dataBundle);
2) And get int in your activity like that :
int defaultValue = 0;
int id = getIntent().getIntExtra(YOUR_KEY, defaultValue);
or
Bundle extras = getIntent().getExtras();
int id = extras.getInt(YOUR_KEY);

That's exaclty what you said
as far I understand, if int = 5 then key will be 5 ? then 'Id' it will be = 5 ?
So ,when you enter in another activity, here
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
You can do int id = getIntent().getIntExtra("id"); , and will be 5

Related

Intent not sending or receiving integer value

public void Show(int n){
Intent in= new Intent(this, Results.class);
in.putExtra("Percent", n); // n is inetger
startActivity(in);`enter code here`
}
Intent intent= getIntent();
// int num = Integer.parseInt(String.valueOf(intent.getStringExtra("Percent")));
String num = intent.getStringExtra("Percent");
int num2= Integer.parseInt(num);
//ch.setText(num);
//String num= in.getStringExtra("Percent");
//int n=
//b.setProgress(num);
t.setText(num2);
You can recieve integer value like this:
Intent intent= getIntent();
int num = intent.getIntExtra("Percent");
You need to use int num intent.getInt("Percent");

Passing variable between activities through intent throws NullPointerException

Why does this throw NullPointerException ?
In Redeem.java
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", yourInt);
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score = extras.getInt("intVariableName");
Try with:
Bundle extras = getIntent().getExtras();
String stringScore = extras.getString("intVariableName");
int score = Integer.parseInt(stringScore);
Or:
int score = intent.getIntExtra("intVariableName", 0);
You can also try this:
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", String.valueOf(yourInt));
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score =Integer.parseInt(extras.getString("intVariableName"));
Use this:
getIntent().getIntExtra("intVariableName", 0); //where 0 is default value

how sent integer parameter in intent

I want to send int parameter in intent like this:
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
and in main class I get that parameter
Intent intent = getIntent();
name = intent.getStringExtra("name");
page1 = Integer.parseInt(intent.getStringExtra("page"));
but when I run my code it force closes!!!
You should use getIntent().getIntExtra(name, defaultValue) instead of Integer.parseInt(intent.getStringExtra("page"));
Update:
int defaultValue = -1;// take any default value of your choice
String name = intent.getStringExtra("name");
int page1 = intent.getIntExtra("page", defaultValue);
I in other class use the main class and send parameter to it and it work without any problem but just in list class i have problem
in other class i like this send parameter
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(list_sub.this,main.class);
i.putExtra("name", Name[position]);
i.putExtra("page", Ted[position]);
startActivity(i);
}
and in refresh class i get ted parameter
private void refresh(){
db.open();
int s = db.List_count("text", ID);
Name= new String[s];
Ted=new String[s];
for(int i=0;i<s;i++){
Name[i]=db.List_display_sub("text", ID, i);
Ted[i]=db.page_count("text", Name[i].toString())+"";
}
db.close();
}
Activity A
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
Activity B
Intent intent = getIntent();
name = intent.getStringExtra("name");
int page = intent.getIntExtra("page", 0);
where 0 is the default value.

Null pointer in putExtras

I have some trouble with putExtras to an intent. Could you please review my code?
public void onSelectCategory(View v) {
int category = Integer.parseInt((String) v.getTag());
Intent intent = new Intent(HomeActivity.this, ListActivity.class);
intent.putExtra("EXT_CATEGORY", category);
startActivity(intent);
}
And in the ListActivity, I'm doing the following..
public static final String EXT_CATEGORY = "category";
int category = getIntent().getExtras().getInt(EXT_CATEGORY);
From this line
intent.putExtra("EXT_CATEGORY", category);
in your another activity String name should be same means:--
public static final String EXT_CATEGORY = "EXT_CATEGORY";
You did several mistakes, here is a overworker version of your code.
public void onSelectCategory(View v) {
int category = Integer.parseInt((String) v.getTag());
Intent intent = new Intent(HomeActivity.this, ListActivity.class);
intent.putExtra(EXT_CATEGORY, category);
startActivity(intent);
}
int defaultCat = -1;
public static final String EXT_CATEGORY = "category";
int category = getIntent().getIntExtra(EXT_CATEGORY,defaultCat); // Use default int if there is no extra
You should use this to get you info, not getExtras() :
int category = getIntent().getIntExtra("EXT_CATEGORY");
getExtras() returns additional Bundle of data. You need only one integer.

Getting Null values while Passing datas through Intent to another activity

I am getting null Values while passing datas through Intent from 1 activity to another activity.
Passing from 1Activity:
int s = position;
String str=adapter.getId(position);
int Type=DeviceType;
Bundle bunch=new Bundle();
bunch.putString("id", str);
bunch.putInt("DeviceType",Type);
bunch.putInt("position", s);
Intent It=new Intent();
It.setClass(PYSActivity.this,GridImages.class);
It.putExtras(bunch);
startActivity(It);
Retriveing here in 2 Activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Bundle b = this.getIntent().getExtras();
AppID=b.getString("id");
DeviceType=b.getInt("DeviceType");
Position=b.getInt("position");
list=(GridView)findViewById(R.id.list);
adapter1=new LazyAdapter1(this,mStrings,like,Rate,Id,img);
list.setAdapter(adapter1);
Do something like this:
Activity 1:
int myInt = 5;
String myString = "hi";
...
Intent Intent = new Intent(...);
intent.putExtra("string_key", myString);
intent.putExtra("int_key", myInt);
startActivity(intent);
Activity 2:
int getInt;
String getString;
...
Bundle extras = getIntent().getExtras();
// Read the extras data if it's available.
if (extras != null)
{
getInt = extras.getInt("int_key");
getString = extras.getString("string_key");
}
Why you are creating bundle just use intent.
it.putExtra("id", str);
it.putExtra("DeviceType",Type);
it.putExtra("position", s);

Categories

Resources