【问题标题】:How to add data to list and use its data on next page and show in listview如何将数据添加到列表并在下一页使用其数据并在列表视图中显示
【发布时间】:2017-03-19 08:17:59
【问题描述】:

我有 2 个班级,一个是 login_class,第二个是 student_class,它显示学生的详细信息。

我必须做的是当我登录时。作为回应,我得到了学生的详细信息,例如 student_name,student_srno,student_father_name,student_mother_name 等。

这是我的 bean(Getter Setter) 类

    package com.smartschoolapp;

public class Bean {

String username, password, srno, studentname, classname, sec, contact,
father_name, mother_name, dob;



public  Bean(String username,String password,String srno,String studentname,String classname,String sec,String contact,String father_name,String mother_name,String dob) {
    this.username=username;
    this.password= password;
    this.srno=srno;
    this.studentname=studentname;
    this.classname=classname;
    this.sec=sec;
    this.contact=contact;
    this.father_name=father_name;
    this.mother_name=mother_name;
    this.dob =dob;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getSrno() {
    return srno;
}

public void setSrno(String srno) {
    this.srno = srno;
}

public String getStudentname() {
    return studentname;
}

public void setStudentname(String studentname) {
    this.studentname = studentname;
}

public String getClassname() {
    return classname;
}

public void setClassname(String classname) {
    this.classname = classname;
}

public String getSec() {
    return sec;
}

public void setSec(String sec) {
    this.sec = sec;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

public String getFather_name() {
    return father_name;
}

public void setFather_name(String father_name) {
    this.father_name = father_name;
}

public String getMother_name() {
    return mother_name;
}

public void setMother_name(String mother_name) {
    this.mother_name = mother_name;
}

public String getDob() {
    return dob;
}

public void setDob(String dob) {
    this.dob = dob;
}

public Bean() {

}
}

这是我的登录类,我在列表中添加学生的数据。

   class login_class extends activity{  
 Bean beanobj;
 List student_detail_list;
 .....
   {
        String name = getting data from response;
        ......... so on

          beanobj = new Bean();
        beanobj.studentname(name);
        beanobj.student_parent_name(parentname);
        .............so on

        }

        order_list.add(beanobj); // the details to list
        }

现在下一个活动 student_class 我想在列表视图中显示学生

public class student_class extends Activity {
ListView student_listview;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.student_list);


    student_listview = (ListView) findViewById(R.id.student_listview);



   }
    }

*==>> 在 login_class 我创建了 List student_detail_list 我如何在我的 student_class 类中获取它并检索数据。 谢谢

【问题讨论】:

    标签: android listview xml-parsing


    【解决方案1】:

    有很多方法可以做到这一点。

    1.推荐的解决方案:How to pass an object from one activity to another on Android
    您可以序列化您的对象并将其放入意图中。这是推荐的解决方案,但这里有一些问题。如果您的数据包含大量内存,它可能会在运行时使您的应用程序崩溃。

    2.您可以将您的数组保存到文件中,将文件路径发送到其他活动,就像使用 URL 一样。在第二个活动中读取文件并在第二个活动中创建数组。

    3. 我最喜欢的解决方案是为学生数组创建一个单例类,在您想要的任何活动中访问该类。但是你应该小心,因为单例模式在设计上有一些问题,它也会一直留在内存中

    【讨论】:

      【解决方案2】:

      实现“类Bean实现Parcelable”。当从“login_class”进入“student_class”时,只需将“student bean data”放入Intent中即可。进入student_class后,可以从intent中获取“学生bean数据”,这里是示例代码:

      Intent intent = new Intent(login_class.this, student_class.class)
      intent.putParcelableArrayListExtra("content", student_detail_list);
      startActivity(intent);
      

      在“学生课堂”中:

      student_detail_list = getIntent().getParcelableArrayListExtra("content");
      

      【讨论】:

        猜你喜欢
        • 2011-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多