0

On a button click I have to send the data that has been stored to a list to an Activity where on that activity I have a list and the list will show the data that has been sent from a previous activity. What I have done so far is:-

In the ActivityOne I have parsed the data and set the data in the listview. In this there is a scenario where there is an JSONArray. I have stored it in a List and got all the data of that particular Object. But on the button click I cant send the list to another activity. Here is my code:-

pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading...Please Wait...");
        pDialog.setCancelable(false);
        pDialog.show();

        Volley.newRequestQueue(getActivity()).add(new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                hidePDialog();
                try {
                    JSONObject result = response.getJSONObject("result");
                    JSONArray jsonArray = result.getJSONArray("course");

                    System.out.println("Course: == >" + result.getJSONArray("course"));
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject trainingObj = jsonArray.getJSONObject(i);
                        movie = new Movie();
                        movie.setCourse_id(trainingObj.getString("course_id"));
                        movie.setParent_course_id(trainingObj.getString("parent_course_id"));
                        movie.setCourse_name(trainingObj.getString("course_name"));
                        movie.setCourse_price(trainingObj.getString("course_price"));
                        movie.setCourse_price_dollar(trainingObj.getString("course_price_doller"));
                        movie.setCourse_price_discount_dollar(trainingObj.getString("course_price_discunt_doller"));
                        movie.setCourse_discount_date(trainingObj.getString("course_discunt_date"));
                        movie.setCourse_description(trainingObj.getString("course_description"));
                        movie.setCourse_image(trainingObj.getString("course_image"));


//                        module = trainingObj.getJSONArray("module");
//                        jsonArray = module.toString();
                        List<Module> mod1=new ArrayList<Module>();

                        JSONArray module = trainingObj.getJSONArray("module");
                        for (int j = 0; j < module.length(); j++) {
                            JSONObject moduleObj = module.getJSONObject(j);
                            modl=new Module();
                            modl.setModule_id(moduleObj.getString("module_id"));
                            modl.setVideo_price(moduleObj.getString("video_price"));
                            modl.setVideo_price_doller(moduleObj.getString("video_price_doller"));
                            modl.setVideo_price_discunt(moduleObj.getString("video_price_discunt"));
                            modl.setVideo_price_discunt_doller(moduleObj.getString("video_price_discunt_doller"));
                            modl.setVideo_discunt_date(moduleObj.getString("video_discunt_date"));
                            modl.setVideo_type(moduleObj.getString("video_type"));
                            modl.setVideo_link(moduleObj.getString("video_link"));
                            modl.setVideo_image(moduleObj.getString("video_image"));
                            modl.setVideo_description(moduleObj.getString("video_description"));
                            mod1.add(j,modl);

                        }
                        movie.setModulelist(mod1);



                        trainingList.add(movie);

All the data has been saved in the setModuleList. Now on a button click from the adapter i have to send the data to another activity. But i am unable to do it.

btn_view_module.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                Intent in = new Intent(activity, ViewModule.class);

                activity.startActivity(in);

            }
        });

The singleton class is given below:-

import java.util.List;

/**
 * Created by Administrator on 2/9/2016.
 */

public class Movie {
    private String course_id;
    private String parent_course_id;
    private String course_name;
    private String course_price;
    private String course_price_dollar;
    private String course_price_discount_dollar;
    private String course_discount_date;
    private String course_description;
    private String course_image;
    private String total_module;
    // Module
    private String module_id;
    private String video_price;
    private String video_price_doller;
    private String video_price_discunt;
    private String video_price_discunt_doller;
    private String video_discunt_date;
    private String video_type;
    private String video_link;
    private String video_image;
    private String video_description;
    private List<Module> modulelist;


    public Movie() {
    }

    public Movie(String course_id, String parent_course_id, String course_name, String course_price,
                 String course_price_dollar, String course_price_discount_dollar, String course_discount_date,
                 String course_description, String course_image, String total_module, String module_id,
                 String video_price, String video_price_doller, String video_price_discunt,
                 String video_price_discunt_doller, String video_discunt_date, String video_type,
                 String video_link, String video_image, String video_description) {
        this.course_id = course_id;
        this.parent_course_id = parent_course_id;
        this.course_name = course_name;
        this.course_price = course_price;
        this.course_price_dollar = course_price_dollar;
        this.course_price_discount_dollar = course_price_discount_dollar;
        this.course_discount_date = course_discount_date;
        this.course_description = course_description;
        this.course_image = course_image;
        this.total_module = total_module;
        this.module_id = module_id;
        this.video_price = video_price;
        this.video_price_doller = video_price_doller;
        this.video_price_discunt = video_price_discunt;
        this.video_price_discunt_doller = video_price_discunt_doller;
        this.video_discunt_date = video_discunt_date;
        this.video_type = video_type;
        this.video_link = video_link;
        this.video_image = video_image;
        this.video_description = video_description;
    }

    public String getCourse_id() {
        return course_id;
    }

    public void setCourse_id(String course_id) {
        this.course_id = course_id;
    }

    public String getParent_course_id() {
        return parent_course_id;
    }

    public void setParent_course_id(String parent_course_id) {
        this.parent_course_id = parent_course_id;
    }

    public String getCourse_name() {
        return course_name;
    }

    public void setCourse_name(String course_name) {
        this.course_name = course_name;
    }

    public String getCourse_price() {
        return course_price;
    }

    public void setCourse_price(String course_price) {
        this.course_price = course_price;
    }

    public String getCourse_price_dollar() {
        return course_price_dollar;
    }

    public void setCourse_price_dollar(String course_price_dollar) {
        this.course_price_dollar = course_price_dollar;
    }

    public String getCourse_price_discount_dollar() {
        return course_price_discount_dollar;
    }

    public void setCourse_price_discount_dollar(String course_price_discount_dollar) {
        this.course_price_discount_dollar = course_price_discount_dollar;
    }

    public String getCourse_discount_date() {
        return course_discount_date;
    }

    public void setCourse_discount_date(String course_discount_date) {
        this.course_discount_date = course_discount_date;
    }

    public String getCourse_description() {
        return course_description;
    }

    public void setCourse_description(String course_description) {
        this.course_description = course_description;
    }

    public String getCourse_image() {
        return course_image;
    }

    public void setCourse_image(String course_image) {
        this.course_image = course_image;
    }

    public String getTotal_module() {
        return total_module;
    }

    public void setTotal_module(String total_module) {
        this.total_module = total_module;
    }

    public String getModule_id() {
        return module_id;
    }

    public void setModule_id(String module_id) {
        this.module_id = module_id;
    }

    public String getVideo_price() {
        return video_price;
    }

    public void setVideo_price(String video_price) {
        this.video_price = video_price;
    }

    public String getVideo_price_doller() {
        return video_price_doller;
    }

    public void setVideo_price_doller(String video_price_doller) {
        this.video_price_doller = video_price_doller;
    }

    public String getVideo_price_discunt() {
        return video_price_discunt;
    }

    public void setVideo_price_discunt(String video_price_discunt) {
        this.video_price_discunt = video_price_discunt;
    }

    public String getVideo_price_discunt_doller() {
        return video_price_discunt_doller;
    }

    public void setVideo_price_discunt_doller(String video_price_discunt_doller) {
        this.video_price_discunt_doller = video_price_discunt_doller;
    }

    public String getVideo_discunt_date() {
        return video_discunt_date;
    }

    public void setVideo_discunt_date(String video_discunt_date) {
        this.video_discunt_date = video_discunt_date;
    }

    public String getVideo_type() {
        return video_type;
    }

    public void setVideo_type(String video_type) {
        this.video_type = video_type;
    }

    public String getVideo_link() {
        return video_link;
    }

    public void setVideo_link(String video_link) {
        this.video_link = video_link;
    }

    public String getVideo_image() {
        return video_image;
    }

    public void setVideo_image(String video_image) {
        this.video_image = video_image;
    }

    public String getVideo_description() {
        return video_description;
    }

    public void setVideo_description(String video_description) {
        this.video_description = video_description;
    }

    public List<Module> getModulelist() {
        return modulelist;
    }

    public void setModulelist(List<Module> modulelist) {
        this.modulelist = modulelist;
    }
}

Module.java

public class Module {

    private String module_id;
    private String video_price;
    private String video_price_doller;
    private String video_price_discunt;
    private String video_price_discunt_doller;
    private String video_discunt_date;
    private String video_type;
    private String video_link;
    private String video_image;
    private String video_description;

    public Module() {
    }

    public Module(String module_id,String video_price, String video_price_doller, String video_price_discunt,
                  String video_price_discunt_doller, String video_discunt_date, String video_type,
                  String video_link, String video_image, String video_description){
        this.module_id = module_id;
        this.video_price = video_price;
        this.video_price_doller = video_price_doller;
        this.video_price_discunt = video_price_discunt;
        this.video_price_discunt_doller = video_price_discunt_doller;
        this.video_discunt_date = video_discunt_date;
        this.video_type = video_type;
        this.video_link = video_link;
        this.video_image = video_image;
        this.video_description = video_description;
    }

    public String getVideo_description() {
        return video_description;
    }

    public void setVideo_description(String video_description) {
        this.video_description = video_description;
    }

    public String getModule_id() {
        return module_id;
    }

    public void setModule_id(String module_id) {
        this.module_id = module_id;
    }

    public String getVideo_price() {
        return video_price;
    }

    public void setVideo_price(String video_price) {
        this.video_price = video_price;
    }

    public String getVideo_price_doller() {
        return video_price_doller;
    }

    public void setVideo_price_doller(String video_price_doller) {
        this.video_price_doller = video_price_doller;
    }

    public String getVideo_price_discunt() {
        return video_price_discunt;
    }

    public void setVideo_price_discunt(String video_price_discunt) {
        this.video_price_discunt = video_price_discunt;
    }

    public String getVideo_price_discunt_doller() {
        return video_price_discunt_doller;
    }

    public void setVideo_price_discunt_doller(String video_price_discunt_doller) {
        this.video_price_discunt_doller = video_price_discunt_doller;
    }

    public String getVideo_discunt_date() {
        return video_discunt_date;
    }

    public void setVideo_discunt_date(String video_discunt_date) {
        this.video_discunt_date = video_discunt_date;
    }

    public String getVideo_type() {
        return video_type;
    }

    public void setVideo_type(String video_type) {
        this.video_type = video_type;
    }

    public String getVideo_link() {
        return video_link;
    }

    public void setVideo_link(String video_link) {
        this.video_link = video_link;
    }

    public String getVideo_image() {
        return video_image;
    }

    public void setVideo_image(String video_image) {
        this.video_image = video_image;
    }

The sample json is provided below:-

"course": [{
            "course_id": "3",
            "parent_course_id": "0",
            "course_name": "PRINCE2 (Foundation and Practitioner)",
            "course_price": "24000",
            "course_price_doller": "140",
            "course_price_discunt": "22000",
            "course_price_discunt_doller": "220",
            "course_discunt_date": "2016-04-04",
            "course_description": "We are an expert training organization with our faculty having vast experience in consulting and training accredited by Peoplecert on behalf of Axelos.\r\nThis program is for 3 full days.\r\n\r\nXellentro has one of the best Project Management Professionals with large experience of project management training and consulting in this subject across the globe.",
            "course_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png",
            "total_module": 3,
            "module": [{
                "module_id": "14",
                "video_price": "12",
                "video_price_doller": "11",
                "video_price_discunt": "123",
                "video_price_discunt_doller": "12",
                "video_discunt_date": "2016-02-03",
                "video_type": "1",
                "video_link": "arrisofttech.com\/2016\/xellentroapp\/video\/sample_2.mp4",
                "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png",
                "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
            }, {
                "module_id": "12",
                "video_price": "12",
                "video_price_doller": "11",
                "video_price_discunt": "123",
                "video_price_discunt_doller": "12",
                "video_discunt_date": "2016-02-03",
                "video_type": "0",
                "video_link": "https:\/\/www.youtube.com\/watch?v=QSaWoca3SjY&list=RDQSaWoca3SjY",
                "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png",
                "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
            }, {
                "module_id": "13",
                "video_price": "12",
                "video_price_doller": "11",
                "video_price_discunt": "123",
                "video_price_discunt_doller": "12",
                "video_discunt_date": "2016-02-03",
                "video_type": "0",
                "video_link": "https:\/\/www.youtube.com\/watch?v=QSaWoca3SjY&list=RDQSaWoca3SjY",
                "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png",
                "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
            }]

Please help me out.

Kanishka Sen
  • 205
  • 2
  • 9

3 Answers3

0

The short answer is you cannot directly send the list which you have in your activity to a new activity. You can send limited amounts of data within your activities using this method.

If that is difficult, then you can also use a singleton object and share the variables.

Community
  • 1
  • 1
Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24
0

Make your class implement Serializable. You can pass an ArrayList, if the E type is Serializable. Do it using:

intent.putExtra("modulelist", setModuleList);

and to get it in the other activity use:

ArrayList<String> moduleList = (ArrayList<String>) getIntent().getSerializableExtra("modulelist");

Check this out.

Community
  • 1
  • 1
Anoop Kanyan
  • 618
  • 7
  • 19
0

Take out your public constructor in your Singleton and replace with a private constructor. Then add a static getInstance() method as follows.

static Module instance = null;//Declare instance variable
private Module(){

}

public static Module getInstance(){

if(instance != null){
   return instance;
}
else{

instance = new Module();
   return instance;
}

}

Now, from anywhere else use the following to get a hold to your instance.

Module singletonModule = Module.getInstance();

Now you can perform any method on the instance.

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24