开发者

How to save List<UserDefinedClass> of between Activity switching?

How to save L开发者_开发问答ist<UserDefinedClass> of between Activity switching ? I get data in onClickView function. Does UserDefinedClass need to implements some interface ?


I believe the best way would be to keep the list in the Application context.

In any of your activitys call yourApp using:

((yourApp)getApplication()).//any public function

public yourApp extends Application{
    private List<UserDefinedClass> yourList;

    //create functions to manipulate the list example:
    public void add(UserDefinedClass a){
        //add this to list
    }

    //finally function to access list
    public List<UserDefinedClass> getList(){
        return yourList;
    }
}

//example:
List<UserDefinedClass> aList = ((yourApp)getApplication()).getList();


It is best to only pass an identifier to the next Activity instead of passing your entire list.

UserDefinedClass should not implement some interface, rather UserDefinedClass should be the class type of the List. In other words, every item in your list should be of UserDefinedClass. That can be a String or even a custom class.


Yes you can pass it if it implements Parcelable. However if its a large amount of data a database or file may work out better for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜