I'm using RecyclerView to populate CardViews with my object "Income". Now I need one of the buttons in CardView to start new Activity and send that object to it.
Here's part of my button's onClickListener in Adapter:
holder.editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editAt(income);
}
});
public void editAt(Income income){
Intent i = new Intent(context,IncomeAddActivity.class);
Bundle bundle = new Bundle();
// here I want to send that "income" object
i.putExtras(bundle);
startActivity(i);
}
Is there any easy method to do this, or my approach is totally wrong?
You need to make your class Income implements the interface Serializable. Then you can do this:
Bundle bundle = new Bundle();
bundle.putSerializable("object", income);
intent.putExtras(bundle);
startActivity(i);
You can also use Shared Preferences to store and get some data
SharedPreferences.Editor ed = mPrefs.edit();
ed.putInt("view_mode", mCurViewMode);
ed.commit();
Related
Hey I have an arraylist in activity1.class I want to get data from that array to show the data episodeList.size() in activity2.class here my list code
public class EpisodeAdapter extends RecyclerView.Adapter<EpisodeAdapter.EpisodeHolder>{
private List<Episode> episodeList;
public EpisodeAdapter(List<Episode> episodeList) {
this.episodeList = episodeList;
}
#Override
public EpisodeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_episode , null);
EpisodeAdapter.EpisodeHolder mh = new EpisodeAdapter.EpisodeHolder(v);
return mh;
}
public int getItemCount() {
return episodeList.size();
}
Intent intent = new Intent(this, className);
intent.putParcelableArrayListExtra("episodeList", episodeList);
startActivity(intent);
and you can get it by
ArrayList<Episodes> list=getIntent.getparcelableArrayList("episodeList")
and don't forget to make the model class to parcelable
You can use intents to pass values from one activity to another activity like this:
In your current activity from where you want to pass ArrayList:
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.putStringArrayListExtra("key", your_array_list);
startActivity();
And in your Activity where you want to receive the values:
Intent intent = getIntent();
ArrayList arrayList= intent.getStringArrayListExtra("key");
I also had the same problem on my project. I just used SharedPreferences. It's like a local file in your project, but you cannot see it. You simply put the values inside and retrieve them in your 2nd Activity.
For more information: https://developer.android.com/training/data-storage/shared-preferences
Implement Parcelable in your ArrayList model.
Intent intent = new Intent(context, ServiceDetailActivity.class);
intent.putParcelableArrayListExtra("key", (ArrayList<? extends Parcelable>) arrayList);
startActivity(intent);
And get list in another activity
arrayList = getIntent().getParcelableArrayListExtra("key");
How can I receive a custom ArrayList from another Activity via Intent? For example, I have this ArrayList in Activity A:
ArrayList<Song> songs;
How could I get this list inside Activity B?
The first part to understand is that you pass information from Activity A to Activity B using an Intent object, inside which you can put "extras". The complete listing of what you can put inside an Intent is available here: https://developer.android.com/reference/android/content/Intent.html (see the various putExtra() methods, as well as the putFooExtra() methods below).
Since you are trying to pass an ArrayList<Song>, you have two options.
The first, and the best, is to use putParcelableArrayListExtra(). To use this, the Song class must implement the Parcelable interface. If you control the source code of Song, implementing Parcelable is relatively easy. Your code might look like this:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putParcelableArrayListExtra("songs", songs);
The second is to use the version of putExtra() that accepts a Serializable object. You should only use this option when you do not control the source code of Song, and therefore cannot implement Parcelable. Your code might look like this:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putSerializableExtra("songs", songs);
So that's how you put the data into the Intent in Activity A. How do you get the data out of the Intent in Activity B?
It depends on which option you selected above. If you chose the first, you will write something that looks like this:
List<Song> mySongs = getIntent().getParcelableArrayListExtra("songs");
If you chose the second, you will write something that looks like this:
List<Song> mySongs = (List<Song>) getIntent().getSerializableExtra("songs");
The advantage of the first technique is that it is faster (in terms of your app's performance for the user) and it takes up less space (in terms of the size of the data you're passing around).
Misam is sending list of Songs so it can not use plain putStringArrayList(). Instead, Song class has to implement Parcelable interface. I already explained how to implement Parcelable painless in post here.
After implementing Parcelable interface just follow Uddhavs answer with small modifications:
// First activity, adding to bundle
bundle.putParcelableArrayListExtra("myArrayListKey", arrayList);
// Second activity, reading from bundle
ArrayList<Song> list = getIntent().getParcelableArrayListExtra("myArrayListKey");
I hope this helps you.
1. Your Song class should be implements Parcelable Class
public class Song implements Parcelable {
//Your setter and getter methods
}
2. Put your arraylist to putParcelableArrayListExtra()
public class ActivityA extends AppCompatActivity {
ArrayList<Song> songs;
#Override
protected void onCreate(Bundle savedInstanceState) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), ActivityB.class)
.putParcelableArrayListExtra("songs", (ArrayList<? extends Parcelable>) songs));
}
});
}
3. In the ActivityB
public class ActivityB extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
final ArrayList<Song> songs = intent.getParcelableArrayListExtra("songs");
//Check the value in the console
buttonCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (Song value : songs) {
System.out.println(value.getTitle());
}
}
});
}
to send a string arrayList in Java you can use,
intent.putStringArrayListExtra("key", skillist <- your arraylist);
and
List<String> listName = getIntent().getStringArrayListExtra("key");
Please note, bundle is one of the key components in Android system that is used for inter-component communications. All you have to think is how you can use put your Array inside that bundle.
Sending side (Activity A)
Intent intent1 = new Intent(MainActivity.this, NextActivity.class);
Bundle bundle = new Bundle();
Parcelable[] arrayList = new Parcelable[10];
/* Note: you have to use writeToParcel() method to write different parameters values of your Song object */
/* you can add more string values in your arrayList */
bundle.putParcelableArray("myParcelableArray", arrayList);
intent1.putExtra("myBundle", bundle);
startActivity(intent1);
Receiving side (Activity B)
Bundle bundle2 = getIntent().getBundleExtra("myBundle"); /* you got the passsed bundle */
Parcelable[] arrayList2 = bundle.getParcelableArray("myParcelableArray");
hi all i am new to android development my question is that how to call input edit text data in any activity where i required just like local storage in hmtl5 how to set value and get them where i required in any activity but not in the intent activity where i mentioned in the below example i want to show in some other activity means i need to get the values
here is my code
public class Autoinput extends Activity {
EditText Engines, Drivers;
Button next;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autoinputvalues);
Engines = (EditText) findViewById(R.id.noofengines);
Drivers = (EditText) findViewById(R.id.noofdrivers);
next = (Button) findViewById(R.id.autoinputnext);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent autoinputscreen = new Intent (getApplicationContext(), autocoverage.class);
startActivity(autoinputscreen);
}
});
}
}
if we write put extra in intent it will go to that particular activity which we mention intent and there only we can get it my intention is to call in any activity and show them.
You should use shared preference. You should refer this link
You can do this in two ways, one is using Bundle and the another one throught Intent.
Using Bundle,
To Send:
Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("Key", "Value");
passIntent.putExtras(bundle);
startActivity(passIntent);
To Receive:
Bundle bundle = getIntent().getExtras();
String recText = bundle.getString("Key");
Using Through Intent:
To Send:
Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
passIntent.putExtras("Key", "Value");
startActivity(passIntent);
To Receive:
String recText = getIntent().getExtras().getString("Key");
For your Code, in your FirstActivity
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "clicked on item: " + position);
Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
passIntent.putExtras("Key", position);
startActivity(passIntent);
}
});
In SecondActivity,
details.setText(getIntent().getExtras().getString("Key"));
If you want the data to be destroyed after your application is closed or crashed you can use something like global variables: For example refer to this
On the other hand if you want your data to be persisted even after application crash, you can use shared preferences:
SharedPreferences pref;
pref=context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor=pref.edit();
Set data:
editor.putString("Engines", Engines.getText().toString());
editor.putString("Drivers", Drivers.getText().toString());
Then retrieve from anywhere:
String engines=pref.getString(("Engines",null);
String drivers=pref.getString(("Drivers",null);
if you want save your data and required in any activity must use Database or Share preference
To obtain shared preferences, use the following method In your activity:
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To read preferences:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Vboolean IsTurnOn = preferences.getBoolean("Key", Default_Value);
To edit and set preferences
Editor editor = preferences.edit();
editor.putBoolean("Key", Value);
editor.commit();
I have an Activity that uses the following code to retrieve information from another activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
int tok = extras.getInt("Token");
tempToken += tok;
}
This is the Code inside the first other class that sends this information:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Now i have another Activity that also wants to sen information to the Main Activity like so:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Campaign.this, Menu.class);
intent.putExtra("Token", player.tokens);
intent.putExtra("Round", player.round);
intent.putExtra("Rank", player.rank);
intent.putExtra("Score", player.score);
intent.putExtra("Sec", player.secondsTapped);
intent.putExtra("Min", player.minutesTapped);
intent.putExtra("Hour", player.hoursTapped);
intent.putExtra("Day", player.daysTapped);
intent.putExtra("LifeTap", player.tapsInLife);
intent.putExtra("SecTap", player.tapsPerSec);
intent.putExtra("TapRo", player.tapps);
startActivity(intent);
}
});
Now my question is, how do i handle these different extras from multiple Activities inside the one Main Activity?
Thank You for your time :)
There are two ways to solve your problem..
1)
You can pass one boolean value to or and int variable with some value.. And retrieve this in your new Activity and check with boolean value or int value and get correct data correspond to Activity.
2) You can save your all Data in Shared Preference. And get your all Data in any Activity.
you can send one boolean value that data is in first class or second class and in MainActivity check the value and get the correct data
i am new developer in android applications.i would like to save the data using shared preference concept.i am saving the data in one activity and get the same data in another activity.here i would like to send the String a[]={"one","two","three"} one activity to another activity.i have written code as follows
Main1.java
public class Main1 extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences shp=getSharedPreferences("TEXT", 0);
final Editor et=shp.edit();
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String s1=((EditText)findViewById(R.id.editText1)).getText().toString();
et.putString("DATA", s1);
String s2[]={"one","two","three"};
//here i would like to save the string array
et.commit();
Intent it=new Intent(Main1.this,Main2.class);
startActivity(it);
}
});
}
Main2.java
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
String kk=getSharedPreferences("TEXT", 0).getString("DATA", null);
//here i would like to get the string array of Main1.java
((EditText)findViewById(R.id.editText1)).setText(kk);
}
can we get the string array values from Main1.java to Main2.java?
Put it into the starting intent:
Intent it = new Intent(Main1.this,Main2.class);
it.putExtra("MY_STRING_ARRAY", s2);
Get it back in the second activity:
String[] myStringArray = getIntent().getStringArrayExtra("MY_STRING_ARRAY");
If you want to send data from one activity to another then the best way would be send data using Intent object's putExtra method
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("data1", "some data");
i.putExtra("data2", "another data");
i.putExtra("data3", "more data");
startActivity(i);
and you can get the data from receiving activity Activity2 like this
Object data1 = getIntent().getExtras().get("data1");
Hope that helps
If you want to save your information via SharedPreference not just pass it along activities, use some code like this:
SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("string_preference", "some_string");
prefEditor.putInt("int_preference", 18);
prefEditor.commit();
The commit command is the responsable of actually saving data to SharedPreferences.