在按钮上单击我必须将已存储到列表中的数据发送到活动,在该活动中我有一个列表,列表将显示从上一个活动发送的数据。到目前为止我所做的是: -
在ActivityOne中,我解析了数据并在listview中设置了数据。在这种情况下,存在JSONArray。我已将其存储在List中并获取该特定Object的所有数据。但是在按钮上单击我无法将列表发送到另一个活动。 这是我的代码: -
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 :(得分:0)
简短的回答是,您无法直接将活动中的列表发送到新活动。您可以使用this方法在活动中发送有限数量的数据。
如果这很困难,那么你也可以使用单例对象并共享变量。
答案 1 :(得分:0)
让您的类实现Serializable。如果E类型是Serializable,则可以传递ArrayList。使用以下方式做到:
intent.putExtra("modulelist", setModuleList);
并在其他活动中使用:
ArrayList<String> moduleList = (ArrayList<String>) getIntent().getSerializableExtra("modulelist");
选中out。
答案 2 :(得分: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();
现在您可以在实例上执行任何方法。