How to send String[] imageUrl to another activity - android

I am having array of image URL.On button click I want to send selected image URL to another activity.
Main.java
String[] imageUrl={"https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png", "https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png"};
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("******");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent)
}
});
OpenImage.java
ImageView image = (ImageView)findViewById(R.id.imageview);
What to write here next

In your First Activity,
String[] data = {"Hello", "World"};
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("some_key", data);
startActivity(intent);
Then in you Sencon Activity,
// At class level
private static final String TAG = SecondActivity.class.getSimpleName();
// In onCreate
String[] data = getIntent().getExtras().getStringArray("some_key");
for (String x : data) {
Log.i(TAG, x);
// Toast to display all you values one by one
Toast.makeText(SecondActivity.this, x, Toast.LENGTH_SHORT).show();
}
Hope this helps...:)

Try This:
Bundle bundel = new Bundle();
bundel.putStringArray("key",array);
Intent intent = new Intent(this,next.class)
intent.putExtras(bundel);
startActivity(intent);

or just
intent.putExtra("strings", myStrings);
the putExtra has many overloads, pass array of primitive type is one of them :)

Use This ti send another Activity...
Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
Bundle bundle = new Bundle();
bundle.putStringArray("ArrayURL", imageUrl);
intent1.putExtras(bundle);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1);
and For Getting
Bundle b = getArguments();
Cat_Name = b.getStringArray("ArrayURL");

Related

How do I create, pass and add values to an int array in my activities

I'm new in programming and I recently encountered a problem in activities. I couldn't pass my int array to multiple activities.
Here is my First Activity :
public void Start(View view) {
Intent i = new Intent(this, Situation1.class);
i.putExtra("arr", new int[]{0, 0, 0, 0, 0, 0});
startActivity(i);
}
Here is my Situation1:
public void Serious(View view)
{
Intent intent = new Intent(this, Situation2.class);
Intent i = getIntent();
int[] arr = getIntent().getIntArrayExtra("arr");
arr[2]=arr[2]+1;
arr[1]=arr[1]+1;
startActivity(intent);
}
Situation2:
public void India(View view)
{
Intent intent = new Intent(this, Situation3.class);
Intent i = getIntent();
Bundle b = i.getExtras();
int[] arr=i.getIntArrayExtra("arr");
arr[0]=arr[0]+1;
startActivity(intent);
}
Thank you for the help!
In Situation1 and Serious function, you didn't put your array in intent which you start the next activity with.
public void Serious(View view)
{
Intent intent = new Intent(this, Situation2.class);
Intent i = getIntent();
int[] arr = getIntent().getIntArrayExtra("arr");
arr[2]=arr[2]+1;
arr[1]=arr[1]+1;
intent.putExtra("arr", arr);
startActivity(intent);
}
You can use arraylist of integer like this:
This code sends array of integer values
Initialize array List
List<Integer> test = new ArrayList<Integer>();
Add values to array List
test.add(1);
test.add(2);
test.add(3);
Intent intent=new Intent(this, targetActivty.class);
Send the array list values to target activity
intent.putIntegerArrayListExtra("test", (ArrayList<Integer>) test);
startActivity(intent);
here you get values on targetActivty
Intent intent=getIntent();
ArrayList<String> test = intent.getStringArrayListExtra("test");
For more information:
http://stackoverflow.com/questions/3848148/sending-arrays-with-intent-putextra
While passing:
Bundle b=new Bundle();
b.putIntArray("arr", new int[]{0, 0, 0, 0, 0, 0});
Intent i = new Intent(this, Situation1.class);
i.putExtras(b);
startActivity(i);
While getting (most probably in oncreate of Situation1 activity):
Bundle b=this.getIntent().getExtras();
int[] array=b.getIntArray("arr");

Transfering data from an activity to another and recomposing the name of varibles

I have menu Menu 1 containing 20 buttons bt1, bt2,..btn when you click a button should go to the main activity Main1 where a title and a text should be displayed. Main1 is extended to Text1 where the titles and texts are.
What's needed is that when you click on button2 for example the Main1 should display Title2 and Text2 and so on.
I did this in the Menu 1:
#Override
public void onClick(View v) {
//do common code here
Bundle bundle1 = new Bundle();
bundle1.putString("somekey1", act1);
Intent i = new Intent(getApplicationContext(), Main1.class);
i.putExtras(bundle1);
startActivity(i);
str= v.getResources().getResourceName(v.getId());
act1= Integer.toString(Integer.parseInt(str.substring(str.indexOf("bt")+2 )));
in the Main1 I did this:
num = Integer.parseInt(getIntent().getExtras().getString("somekey1"));
stringId1 = getResources().getIdentifier("title"+num, "string", getPackageName());
stringId2 = getResources().getIdentifier("text"+num, "string", getPackageName());
if (stringId1 > 0) {
title=getString(stringId1);
text2=getString(stringId2);
}
in the Text1 I did this:
public class Text extends Activity {
public String
title1="some title",
text1="some text",
title2="some title",
text2="some text",
titl3="333",
tex3="kar3",
title4="xxxxx",
text4="xxxxx",
title5="",
text5="",
But all of that doesn't work, and about to shake my head on the wall, as the bundle doesn't transfer the data, and stringId1 = getResources().getIdentifier("title"+num, "string", getPackageName());
also returns 0.
Help please`
Pass the intent as shown below:
Intent intent = new Intent(getApplicationContext(),Main1.class);
intent.putExtra("Key1", "Value1");
intent.putExtra("Key2", "Value2");
startActivity(intent);
And in the oncreate() of your Main1.class :
Intent dataIntent = getIntent();
String value1 = dataIntent.getStringExtra("Key1");
String value2 = dataIntent.getStringExtra("Key2");
Please try the following
#Override
public void onClick(View v) {
//do common code here
Bundle bundle1 = new Bundle();
str= v.getResources().getResourceName(v.getId());
act1= Integer.toString(Integer.parseInt(str.substring(str.indexOf("bt")+2 )));
bundle1.putString("somekey1", act1);
Intent i = new Intent(getApplicationContext(), Main1.class);
i.putExtras(bundle1);
startActivity(i);

passing int into android activities

i pass int to next activity using this code
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
but when in this activity, it always display 0.
Intent intent = new Intent();
You are creating a new intent instead of using the one passed to your ActivityB. So use
Intent intent = getIntent();
instead;
use this int i = getIntent().getIntExtra("selectedType", 0);
try getIntent().getExtras().getInt("selectedType")
Try now,
int value = getIntent().getExtras().getInt("selectedType");
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
Intent intent = new getIntent();
^^^^^^^^^
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
Because you're creating a new intent and trying to get "selectedType" on it. But that intent has just been created, so it hasn't that value that you seek.
Try to getIntent() method to get your calling intent, which has your "selectedType" value...
Here's a snap:
Bundle extras = getIntent().getExtras();
if(extras != null) {
int value = extras.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(value), Toast.LENGTH_LONG).show();
}
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
This is wrong. You are creating a new intent object. To get the intent object that was used to start this activity use getIntent() method.
Intent intent = getIntent();
int i = intent.getIntExtra("seelctedType", 0);
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType",i);
startActivity(intent);
And receiving..
if (getIntent().getExtras().containsKey("selectedType")) {
int message = getIntent().getIntExtra("selectedType");
Toast.makeText(ReceiverActivity.this, "" + message, Toast.LENGTH_LONG)
.show();
}

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);

How to pass integer from one Activity to another?

I would like to pass a new value for an integer from one Activity to another.
i.e.:
Activity B contains an
integer[] pics = { R.drawable.1, R.drawable.2, R.drawable.3}
I would like activity A to pass a new value to activity B:
integer[] pics = { R.drawable.a, R.drawable.b, R.drawable.c}
So that somehow through
private void startSwitcher() {
Intent myIntent = new Intent(A.this, B.class);
startActivity(myIntent);
}
I can set this integer value.
I know this can be done somehow with a bundle, but I am not sure how I could get these values passed from Activity A to Activity B.
It's simple. On the sender side, use Intent.putExtra:
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
On the receiver side, use Intent.getIntExtra:
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Their are two methods you can use to pass an integer. One is as shown below.
A.class
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
B.class
Intent intent = getIntent();
int intValue = intent.getIntExtra("intVariableName", 0);
The other method converts the integer to a string and uses the following code.
A.class
Intent intent = new Intent(A.this, B.class);
Bundle extras = new Bundle();
extras.putString("StringVariableName", intValue + "");
intent.putExtras(extras);
startActivity(intent);
The code above will pass your integer value as a string to class B. On class B, get the string value and convert again as an integer as shown below.
B.class
Bundle extras = getIntent().getExtras();
String stringVariableName = extras.getString("StringVariableName");
int intVariableName = Integer.parseInt(stringVariableName);
In Activity A
private void startSwitcher() {
int yourInt = 200;
Intent myIntent = new Intent(A.this, B.class);
intent.putExtra("yourIntName", yourInt);
startActivity(myIntent);
}
in Activity B
int score = getIntent().getIntExtra("yourIntName", 0);
In Sender Activity Side:
Intent passIntent = new Intent(getApplicationContext(), "ActivityName".class);
passIntent.putExtra("value", integerValue);
startActivity(passIntent);
In Receiver Activity Side:
int receiveValue = getIntent().getIntExtra("value", 0);

Categories

Resources