Android studio how to pass Array data to another activity's TextView? - android

I am new to learning Array in android studio. Please show me some examples in details. I have write an example here and I want to display the Array data from MainActivity into second_page activity .
MainActivity.java
public class MainActivity extends AppCompatActivity {
String my_array[]={"dog","cat","tiger"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void next_page(View view){
Intent intent = new Intent(this,second_page.class);
intent.putExtra("my_array_next", my_array);
startActivity(intent);
}
}
second_page.java
public class second_page extends MainActivity {
TextView get_data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_page);
get_data=(TextView)findViewById(R.id.tv);
Intent intent=getIntent();
// coding here to display the array data
// sth like abc.setText(display_array_data);
}
Please advice. Thank you!

If you are trying to send a String-array from one Activity to another this can be done in the Intent.
In ClassA:
Intent intent = new Intent(this, ClassB);
String[] my_array = new String[]{"dog","cat","tiger"};
intent.putExtra("myArr", my_array);
startActivity(intent);
In ClassB:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("myArr");
}
this may helps you

In second_page.java, receive the array you pass via Intent and set it to your TextView like this
#Override
protected void onCreate(Bundle savedInstanceState) {
...
String[] array = intent.getStringArrayExtra("my_array_next");
// TextView display a String so you should convert your Array to String
String str1 = Arrays.toString(array);
get_data.setText(str1);
}

First take the array:
Intent intent = getIntent();
List array;
if (intent.getExtras() != null) {
array= intent.getExtras().getBoolean("my_array_next");
}
Then print
get_data.setText(array.toString());

Sending Class:
Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);
Reciving Class:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
}

In your Second activity:
String[] array=getIntent().getStringArrayExtra("my_array_next");
I think you need to go through basics, go to https://developer.android.com/index.html to get started.

Related

How to pass intent from Adapter class to other activity but my variable is in other activity

I have a problem. I want to pass my intent String "EmailHolder" from "WarehouseActivity" to "ProfilWarehouseActivity" by click on a recyclerview item in my Adapter Class which is named as "WarehouseAdapter". I want to pass the EmailHolder to fill my data when the user updates data inside ProfilWarehouseActivity.
Here is the "Intent" from my "WarehouseActivity" class :
private String EmailHolder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_warehouse);
Bundle extras = getIntent().getExtras();
EmailHolder = extras.getString("emailuser");
Here is how I make intent in Adapter Class "WarehouseAdapter" to pass ID data into "ProfilWarehouseActivity" :
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull WarehouseAdapter.ViewHolder holder, final int position) {
holder.namaitem.setText(listItems.get(position).get(Konfigurasi_Warehouse.TAG_WAREHOUSENAMA));
holder.stockitem.setText("Stock : "+listItems.get(position).get(Konfigurasi_Warehouse.TAG_WAREHOUSESTOCK));
holder.merekitem.setText("Merek : "+listItems.get(position).get(Konfigurasi_Warehouse.TAG_WAREHOUSEMEREK));
holder.tglin.setText("Tanggal Masuk : "+listItems.get(position).get(Konfigurasi_Warehouse.TAG_WAREHOUSETGLIN));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
String idItem = listItems.get(position).get(Konfigurasi.TAG_ITEMID);
passid(idItem);
}
});
}
private void passid(String idItem) {
Intent intent = new Intent(context, ProfilWarehouseActivity.class);
intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID,idItem);
//i think this is for get my EmailHolder from WarehouseActivity to pass it with Intent from this Adapter class
context.startActivity(intent);
}
My question is How to pass Intent with a string "EmailHolder" that contain Intent value through my WarehouseAdapter (Adapter class), but "EmailHolder" is from my WarehouseActivity?
EDIT : Here is how i used the Adapter class from my WarehouseActivity
final WarehouseAdapter mAdapter = new WarehouseAdapter( WarehouseActivity.this,list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(WarehouseActivity.this));
mRecyclerView.setAdapter(mAdapter);
You can have couple of options to solve this:
Option 1:
If you want to start "ProfilWarehouseActivity" activity from the adapter, then you need to pass the "EmailHolder" from "WarehouseActivity" to your adapter by either its constructor, or a setter:
Passing String into Adapter Constructor
final WarehouseAdapter mAdapter = new WarehouseAdapter( WarehouseActivity.this, list, EmailHolder);
Passing String using a Setter
In "ProfilWarehouseActivity" activity:
mAdapter.setEmailHolder(EmailHolder);
In WarehouseAdapter adapter:
private String mEmailHolder;
public void setEmailHolder(String emailHolder) {
this.mEmailHolder = emailHolder;
}
....
private void passid(String idItem) {
Intent intent = new Intent(context, ProfilWarehouseActivity.class);
intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID, idItem);
intent.putExtra("EmailHolder", mEmailHolder);
//i think this is for get my EmailHolder from WarehouseActivity to pass it with Intent from this Adapter class
context.startActivity(intent);
}
Option 2:
The other option is to create a listener and implement it in "ProfilWarehouseActivity", And pass the itemId as a parameter to the listener callback. Then let your "ProfilWarehouseActivity" call the "WarehouseActivity" instead of the WarehouseAdapter whenever this listener is triggered.
In WarehouseAdapter adapter:
public interface ItemClickListener {
public void onItemClick(int idItem);
}
ItemClickListener mItemClickListener;
public void setItemClickListener(ItemClickListener listener) {
mItemClickListener = listener;
}
private void passid(String idItem) {
if (mItemClickListener != null)
mItemClickListener.onItemClick(idItem);
}
In "ProfilWarehouseActivity" activity:
class ProfilWarehouseActivity extends AppCompatActivity implements WarehouseAdapter.ItemClickListener {
private String EmailHolder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_warehouse);
Bundle extras = getIntent().getExtras();
EmailHolder = extras.getString("emailuser");
}
#Override
onItemClick(int itemId) {
Intent intent = new Intent(this, ProfilWarehouseActivity.class);
intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID, idItem);
intent.putExtra("EmailHolder", EmailHolder);
startActivity(intent);
}
...
final WarehouseAdapter mAdapter = new WarehouseAdapter( WarehouseActivity.this,list);
mAdapter.setItemClickListener(this);
Pass the emailHolder value to your Adapter while creating your Adapter constructor. If I am not wrong, you might be calling the Adapater from your Activity, so just pass the value as constructor parameter.
wareHouseAdapter = new new WarehouseAdapter( WarehouseActivity.this,list,EmailHolder);
And in the WarehouseAdapter
WarehouseAdapter(otherparameter,String emailHolder) {
this.emailHolder= emailHolder;
}
And on clicking the recyclerView item
private void passid(String idItem) {
Intent intent = new Intent(context, ProfilWarehouseActivity.class);
intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID,idItem);
//i think this is for get my EmailHolder from WarehouseActivity to pass it with Intent from this Adapter class
intent.putExtra("emailHolder",emailHolder);
context.startActivity(intent);
}
i think i accidently found the answer (inspired from the other answer before me, thank you)... i just need to set String "EmailHolder" in "WarehouseActivity" to "public static" :
public static String EmailHolder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_warehouse);
mRecyclerView = findViewById(R.id.recyclerViewItemWar);
Bundle extras = getIntent().getExtras();
EmailHolder = extras.getString("emailuser");
And then use intent from "WarehouseAdapter" like this :
private void passid(String idItem) {
Intent intent = new Intent(context, ProfilWarehouseActivity.class);
intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID,idItem);
intent.putExtra("emailuser",WarehouseActivity.EmailHolder); //i add this
context.startActivity(intent);
}
is it fine to change String from "private" to "public static"..?
For that you have use "interface " You cant directly pass value in Constructor also.

Android: Dynamically starting an activity

I would like to dynamically start an activity based on the previous activity's input. I have input a string through the previous activity, the only thing is this specific code throws the error
cannot resolve constructor 'Intent(com.MentalMathWorkout.EasyCountDown, java.lang.String)'
Is there a way to make this work?
public class EasyCountDown extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ecd);
Intent intent = getIntent();
String test = intent.getStringExtra(MainActivity.TEST_TYPE);
String cstring = ".class";
final String activity = test.concat(cstring);
Intent intent = new Intent(EasyCountDown.this, activity);
startActivity(intent); //Start test
}
The ComponentName object does just that:
String activity = intent.getStringExtra(MainActivity.TEST_TYPE);
Intent intent = new Intent(this, new ComponentName(this, activity));
startActivity(intent);
That's assuming this is an instance of Activity. (for a Fragment, use getActivity(), obv.)
I have a class on here:
com.yasinkacmaz.newproject.activity.ProfileActivity
My test string like that:
"com.yasinkacmaz.newproject.activity.ProfileActivity"
And it working good:
public class EasyCountDown extends AppCompatActivity {
final Activity thisActivity = this;
private Intent previousIntent,nextIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
previousIntent = getIntent();
String test = previousIntent.getStringExtra(MainActivity.TEST_TYPE);
final String activity = test;
Class newclass = null;
try {
newclass = Class.forName(activity);
} catch (ClassNotFoundException c) {
c.printStackTrace();
}
if(newclasss != null) {
nextIntent = new Intent(thisActivity, newclass);
startActivity(nextIntent);
} else {
Toast.makeText(getApplicationContext(),"new class null",Toast.LENGTH_SHORT).show();
}
}
}
Dont forget you can use switch case or etc., because in this way you can get ClassNotFoundException and your intent will be null.

getIntent() doesn't retrieve previous activity's intent and return null

I am trying to post in an ArrayList of strings from one activity to another when the option menu is pressed. However, when I try to getIntent() in the next page, it returns null value.
Option menu when item is selected code:
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==2){
ArrayList<String>data1=new ArrayList<>();
data1.add("Hello");
data1.add("bye");
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("dataList",data1);
startActivity(i);
}
return true;
}
Retrieve Data Activity :
public class SecondActivity extends AppCompatActivity {
ListView lListView;
ArrayAdapter<String> adapter;
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lListView = (ListView)findViewById(R.id.lListView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
lListView.setAdapter(adapter);
}
}
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
add this lines in oncreate
Move this code into your onCreate method:
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
Intent does not exist at the point that you try to get it, it is being passed later in your activity's lifecycle. That is why you are getting null.
Check changes i have made in your code.. getIntent() should be called
inside oncreate...
public class SecondActivity extends AppCompatActivity {
ListView lListView;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//changes
Intent intent = getIntent();
ArrayList<String> data =
intent.getExtras().getStringArrayListExtra("dataList");
//changes
lListView = (ListView)findViewById(R.id.lListView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
lListView.setAdapter(adapter);
}
}
At first set
i.putStringArrayListExtra("dataList",data1);
Then call in Oncreate() section ;
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
Finally
public class SecondActivity extends AppCompatActivity {
ListView lListView;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
lListView = (ListView)findViewById(R.id.lListView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
lListView.setAdapter(adapter);
}
}
You should use i.putStringArrayListExtra("dataList", data1); to set the ArrayList in your code
Try in the other Activity in onCreate():
ArrayList<String> data = (ArrayList<String>) getIntent().getSerializableExtra("dataList");
or
ArrayList<String> data = (ArrayList<String>) getIntent().getParcelableExtra("dataList");

How to display math solutions on a new activity?

I am building an Android app. How can I get the calculations' solutions to be displayed on a new activity?
Make your calculation in Activity A.
And send them to the other activity using intent.putExtra(...)
Take a look in this guide.
On activity A
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("YourKeyWord", yourVariableWithValue);
startAcvitity(intent);
On activity B
int result;
Bundle extras = getIntent.getExtras();
if (extras =! null) {
result = extras.getInt("YourKeyWord");
}
Activity A:
public class First extends Activity{
int a,b,c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
a = 1,b = 2;
c = a + b;
Intent intent = new Intent(this,Second.class);
intent.putExtra("result", c);
startActivity(intent);
}
}
Activity B:
public class Second extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int result = intent.getExtras().getInt("result");
}
}

How to get data from other activity in android?

I have two activities such as Activity A and B and I'm trying to pass two different strings from A to B using Bundle and startActivity(intent).
Like that:
Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
bundle.putString("vidoedetails", filedetails);
//bundle.putString("videoname", filename);
intent.putExtras(bundle);
//intent.putExtra("videofilename", filename);
//intent.putExtra("vidoefiledetails", filedetails);
startActivity(intent);
And in class B I'm using two TextViews to display the strings from class A seperately.
Like that:
Intent i = getIntent();
Bundle extras = i.getExtras();
filedetails = extras.getString("videodetails");
filename = extras.getString("videoname");
The problem is filedetils get printed in class B but not the file name.
Any solution for this?
you have a typo:
bundle.putString("vidoedetails", filedetails);
should be
bundle.putString("videodetails", filedetails);
I know I am 9 days late on this answer, but this is a good example of why I create a constants class. With a constants class, it doesnt matter if it is misspelled ("video" -> "vidoe") because it will be 'misspelled' in both places as you are referencing it through a well known location.
Constants.java
public static String WELL_KNOWN_STRING "org.example.stackoverflow.4792829";
Activity1.java
bundle.putString(Constants.WELL_KNOWN_STRING, filedetails);
Activity2.java
filedetails = extras.getString(Constants.WELL_KNOWN_STRING);
Yes, you spelled wrongly videodetails:
Yours: vid*OE*details
Correct: vid*EO*details
// First activity
actvty_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(v.getContext(),SECONDACTIVITY.class);
startActivityForResult(i, STATIC_INTEGER_VALUE);
}
});
/* This function gets the value from the other activity where we have passed a value on calling this activity */
public void activity_value() {
Intent i = getIntent();
Bundle extras=i.getExtras();
if(extras !=null) {
// This is necessary for the retrv_value
rtrv_value = extras.getString("key");
if(!(rtrv_value.isEmpty())) {
// It displays if the retrieved value is not equal to zero
myselection.setText("Your partner says = " + rtrv_value);
}
}
}
// Second activity
myBtn.setOnClickListener(new View.OnClickListener () {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class);
Bundle bundle = new Bundle();
bundle.putString("key", txt1.getText().toString());
// Here key is just the "Reference Name" and txt1 is the EditText value
intent.putExtras(bundle);
startActivity(intent);
}
});
Here's another way to pass data between Activities. This is just an example from a tutorial I was following. I have a splash screen that runs for 5 seconds and then it would kill the sound clip from:
#Override
protected void onPause() {
super.onPause();
ourSong.release();
}
I decided I wanted the sound clip to continue playing into the next activity while still being able to kill/release it from there, so I made the sound clip, MediaPlayer object, public and static, similar to how out in System.out is a public static object. Being new to Android dev but not new to Java dev, I did it this way.
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity {
public static MediaPlayer ourSong; // <----- Created the object to be shared
// this way
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.dubstep);
ourSong.start();
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openStartingPoint = new Intent(
"expectusafterlun.ch.androidtutorial.MENU");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
}
Then from the next activity, or any other activity, I could access that MediaPlayer object.
public class Menu extends ListActivity {
String activities[] = { "Count", "TextPlay", "Email", "Camera", "example4",
"example5", "example6" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,
android.R.layout.simple_expandable_list_item_1, activities));
}
#Override
protected void onPause() {
super.onPause();
Splash.ourSong.release(); // <----- Accessing data from another Activity
// here
}
}

Categories

Resources