简体   繁体   中英

How to send ArrayList of class object type from one activity to another activity?

I want to send ArrayList of class object type from one activity to another activity but, it gives me an error please help me. I have tried many links but, my problem did not solve. FATAL EXCEPTION MAIN: Java.Lang.RuntimeException I have tried these links: Passing ArrayList with objects to new Activity? How to pass ArrayList of Objects from one to another activity using Intent in android?

//Sending Side Code:


                         if(result.size()>0)
                        {

        ArrayList< LogSetGet> lst=new ArrayList<LogSetGet>();
        lst=result;
         Intent i=new Intent(LogIn.this,Home.class);
         i.putExtra("list",result);
         startActivity(i);
                     }

//Receiving side Code:
        Intent i=this.getIntent();
        ArrayList<LogSetGet>ls=ArrayList<LogSetGet>)i.getSerializableExtra("list");
        ArrayAdapter<LogSetGet> adptr=new ArrayAdapter<LogSetGet>(Home.this,android.R.layout.simple_list_item_1,ls);
        lv.setAdapter(adptr);

Thank you in advance.

Just declare your ArrayList like this above onCreate()

static ArrayList< LogSetGet> lst=new ArrayList<LogSetGet>();

When you declare a variable or method you can call where ever you want within your application with Activity name.

For example

ArrayList< LogSetGet> lst1 = new ArrayList<LogSetGet>();
lst1  = youractivityname.lst; 

Your activity name is nothing but where you declare static ArrayList< LogSetGet> lst=new ArrayList();

you write this

class LogSetGet implements Serializable{

}

and

    if(result.size()>0){
    ArrayList< LogSetGet> lst=new ArrayList<LogSetGet>();
    lst=result;
    Intent i=new Intent(LogIn.this,Home.class);
    i.putExtra("list",result);
    startActivity(i);
    }

hope you get this

depends on the type:

putIntegerArrayListExtra(String name, ArrayList<Integer> value)

putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

putStringArrayListExtra(String name, ArrayList<String> value)

putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value)

Then you can read from next activity with get with key string as argument:

myIntent.getStringArrayListExtra("arrayPeople");

If your objects in ArrayList are big or if you will use them in many difference activities I recommend you to use Singleton pattern. Example of simple Singleton:

public class TestSingleton {

private static TestSingleton testSingleton;
private ArrayList<String> testArray;

TestSingleton() {
    testArray = new ArrayList<String>();
}

public static TestSingleton getInstance() {
    if (testSingleton == null)
        testSingleton = new TestSingleton();
    return testSingleton;
}}

Then you will be able to get instance of TestSingleton with getInstance() in any place in your app. There will be only one object in memory and you will be able to work with it without warring how to move it between activities. Just call TestSingleton.getInstance() in any place you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM