send array list through 3 activities using put extra - android

what i have is three activities, the first one called penddingorders which i make an array list inside it and i add strings to it like this :
ArrayList prodlist=new ArrayList();
for(int z=0;z<=mylist.size();z++){
if(id.equals(mylist.get(arg2).getOrderId()))
{
product=mylist.get(arg2).getProductName();
quantity=mylist.get(arg2).getQuantity();
unit=mylist.get(arg2).getUnit();
price=mylist.get(arg2).getPrice();
totalprice=mylist.get(arg2).getTotalPrice();
totalpriceAfterDiscount=mylist.get(arg2).getPriceAfterDiscount();
note=mylist.get(arg2).getNote();
prodlist.add(product);
prodlist.add(quantity);
prodlist.add(unit);
prodlist.add(totalprice);
prodlist.add(price);
prodlist.add(totalpriceAfterDiscount);
prodlist.add(note);
arg2++;
}
and i have print it in the logcat and it shows correctly:
Log.e("product list",prodlist+"");
and i have send it from the first activity to the third activity like this , though i should pass through the second one before i go to the third one :
Intent intent = new Intent(PenddingOrders.this, ProductInfoDetails.class);
intent.putStringArrayListExtra("prodlist", prodlist);
startActivity(intent);
and in the third activity i get the array list through this code :
try{
prodlist = getIntent().getStringArrayListExtra("prodlist");
}
catch(Exception e){
e.printStackTrace();
}
Log.e("prodlist",prodlist+"");
but in the logcat i get null value .. so what i am doing wrong please help ??

you can set your property using getter setter so you can manage your value very well.
and make it parceable. so you can use it Extras ArrayList of it into next Activity.
Please follow this https://stackoverflow.com/a/15731120/942224

This is my ans on stackoverflow please checked and hope it's help alot:
Java / Android ArrayList in different Activity

Better to use Android Application class to persist data.
For reference link
public class MyApplication extends Application {
private ArrayList<String> list;
public void setValue(ArrayList<String> list) {
this.list = list;
}
public ArrayList<String> getValue() {
return list;
}
}
Step-1
First set the value from your Activity
MyApplication globalVariable = (MyApplication) getApplicationContext();
globalVariable.setValue(prodlist)// Set your ArraList that you want to store
Step-2
Retrieve the value in other activity like
MyApplication globalVariable = (MyApplication) getApplicationContext();
// Get ArrayList from global/application context
ArrayList<String> list = globalVariable.getValue();
and don't forget to register in Manifest.xml
<application
android:name="YOUR_PACKAGE_NAME.MyApplication "
-----------------------

Related

How to send and retrieve data of intent to its adapter class

I am a beginner in android studio, so please bear with me.
I've retrieved data from my intent in a recyclerview activity but I don't know how to pass it to its corresponding adapter class (recyclerview_adapter.java).
How should I start, does anybody have an example?
Thanks in advance.
you can just create a constructor inside adapter class with variable you want to pass and place a global variable by which you can use that variable(data).
class adapter{
// you can pass whole object or any kind of data here as well, just replace string with appropriate class name...
private String data;
// the data string will be set from your recycle activity...
public adapter(String data){
this.data = data;
}
// use the data string anywhere in adapter class...
}

How can i transfer ArrayList between Activities?

I'm trying to transfer ArrayList between activities, but nothing I've tried works as well.
This was my best shot, but this didn't work either.
I'm calling the external action here:
getComics getComicInfo = new getComics(charName, pageNum);
getComicInfo.execute();
getIntentData()
and here i'm trying to put data, but the problem is due the fact that this is an external action, so I can't shift through activits.
if(counter == comicInfoObject.length()){
Log.v("check arr length =>" , Integer.toString(comicList.size()));
Intent i = new Intent();
i.putExtra("comicArrayList", (ArrayList<Comics>)comicList);
}
and here i'm tring to retrive the data, but it doesn't get inside the "if"
public void getIntentData(){
Intent i = getIntent();
if(i != null && i.hasExtra("comicArrayList")){
comicList2 = i.getParcelableExtra("comicArrayList");
int size = comicList2.size();
}
}
first code is where I call an external class that using api and in the bottom line creates the arrayList
second code is inside the external class, where I'm trying to pass the arrayList with putExtra
third code is where i'm tring to retrive the data after getIntentData().
Pack at the sender Activity
intent.putParcelableArrayListExtra(<KEY>, ArrayList<Comics extends Parcelable> list);
startActivity(intent);
Extract at the receiver Activity
getIntent().getParcelableArrayListExtra(<KEY>);
You need make a Comics class that implements Parcelable, then use put ParcelableArrayListExtrato pass arraylist.
Here is a sample link for Pass data between activities implements Parcelable
However be careful if your array list too big, you could get intent exception, for this case you could think about a static reference to store your array list.

how to pass the ArrayList<data class> from one activity to another activity android

In my application whenever we click the "SEARCH" button it makes Http call and get the json as response. I parsed the json and save the data in arraylist bdata.
ArrayList bdata = new ArrayList();
BusData contain getters and setters . I also used one adapter class for custom listview.
now i want to set the adapter to listview but the listview is in another class (not in the class where search button is locate) so how to send my arraylist from one activity to another activity with the help of intents for set the adapter to listview.
Thank you.
A way to do this is by serializing your arraylist to a JSON.
Use GSON library
compile 'com.google.code.gson:gson:2.3.1'
Add the above line in your build.gradle file.
Make a class to help you serializing your objects
public class SerializationHelper {
private static Gson gson = new Gson();
public static String serialize(Object object) {
return gson.toJson(object);
}
public static Object deserialize(String json, Class classType) {
return gson.fromJson(json, classType);
}
}
Now when you want to start the new activity add the serialized string.
String json=SerializationHelper.serialize(myArrayList);
intent.putExtra("data",json);
And in your new activity on create get it and create your object again.
String json=intent.getStringExtra("data");
Object deserializedObject=SerializationHelper.deserialize(json,ArrayList.class);
Now cast your object!
ArrayList<MyClass> myCoolArray=(ArrayList<MyClass>)deserializedObject.
Other simpler way is to make your arraylist static and public, and store it in an other class.
public class GlobalStuff{
public static ArrayList<MyClass> myAwesomeList;
}
Now acces your list by GlobalStuff.myAwesomeList.
You need to create custom class (ArrayList<data class>) to Parcelable and then you can pass it to other activity.
http://androidideasblog.blogspot.in/2010/02/passing-list-of-objects-between.html
When passing data through activities you should use Parcelable, the are many examples in stackoverflow, this one is good:
https://stackoverflow.com/a/22446641/2091315
Next, you need to configure the intent, on the "sender" Activity:
Intent intent = new Intent(getApplicationContext(),
ReceiverActivity.class);
intent.putExtra("arrayListIdentifier",parcelableArrayClass);
startActivity(intent);
On the "receiver" Activity:
parcelableArrayClass myParcelableObject = (parcelableArrayClass) getIntent().getParcelableExtra("arrayListIdentifier");

android accessing variables through classes

I have this application with 2 classes, in the first class I define a list array wich I want to access in my second class, how do I do that? class one (with the array extends listActivity and the other class extends Activity). I don't think it's nessecary to post my code as I believe there is a quick solution to this I just don't know it. Allthough I can post it if you can't help me without seeing the actual code.
You can use a custom ApplicationContext to share global state between activities - see here for more details...
you can pass ArrayList<String> 's across activities using intentExtras.
ArrayList<String> myArrayList = new ArrayList<String>();
// populate your array -> if you want to send objects make a string form of them
Intent myIntent = new Intent(this, Activit2.class);
mtIntent.putStringArrayList("my_array_list", myArrayList);
In Activity2
Intent intent = getIntent();
List<String> stringMediaList = intent.getStringArrayListExtra("my_array_list");
for (int i = 0; i < stringMediaList.size(); i++) {
//reconstruct object if needed
}
this will work if you dont want to use parcelable

How to Use ArrayList in more than one Activity?

How i can access ArrayList from one Activity to another and also clear ArrayList value?
you can use setter/getter method for it.
public class MySetGet
{
private ArrayList aList = null;
public void setList ( ArrayList aList )
{
this.aList = a.List;
}
public ArrayList getList ()
{
return aList;
}
}
Now you can set its value from any Activity/Class and get its value from any Activity/Class.
The most simply and possible way to do the desired::
1.Create a simple public class say Data..
Now create a public static your Array list object.
Now access any where..
Data.listObj
2.Create the List object as public static in one activity and use in another via,
SecondActivity.listObj.clear();

Categories

Resources