2016-05-29 76 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

我無法得到它。你能解釋一下嗎? –

相關問題