2016-05-29 66 views
0

在按钮上单击我必须将已存储到列表中的数据发送到活动,在该活动中,我有一个列表,列表将显示已发送的数据从以前的活动。我到目前为止所做的是: -无法将列表发送到其他活动

在ActivityOne中,我已经解析了数据并设置了数据在列表视图中。在这里有一个场景,有一个JSONArray。我将它存储在一个List中并获取了该特定对象的所有数据。但在按钮上单击我不能将列表发送到另一个活动。 这里是我的代码: -

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); 

所有数据已经​​保存在setModuleList。现在在适配器上点击一个按钮,我必须将数据发送到另一个活动。但我无法做到这一点。

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



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

       activity.startActivity(in); 

      } 
     }); 

单例类在下面给出: -

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; 
    } 

样品JSON提供如下: -

"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." 
      }] 

请帮我一把。

回答

0

简短的回答是,您不能直接将您在活动中的列表发送到新活动。您可以使用this方法在您的活动中发送有限数量的数据。

如果这很困难,那么你也可以使用单例对象并共享这些变量。

+0

我正在使用Singleton对象。让我更新代码并与单身人员分享你 –

+0

我无法控制使用它。请建议我该怎么做才能克服这个问题 –

0

让您的类实现Serializable。如果E类型是Serializable,则可以传递一个ArrayList。用做它:

intent.putExtra("modulelist", setModuleList); 

,并把它在其他活动中使用:

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

检查这个out

+0

我已经更新了代码。请看看并建议我该怎么办? –

+0

基本上让你的模块类实现可串行化 –

0

拿出您的Singleton中的公共构造函数,并用一个私有构造函数替换。然后添加一个静态getInstance()方法如下。

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

} 

public static Module getInstance(){ 

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

instance = new Module(); 
    return instance; 
} 

} 

现在,从其他任何地方使用以下方法来暂缓实例。

Module singletonModule = Module.getInstance(); 

现在您可以对该实例执行任何方法。

+0

我无法得到它。你能解释一下吗? –

相关问题