可序列化列表只读取一个对象。
班级部门
public class Department implements Serializable {
int Id;
String DepartmentName;
int Status;
public Department(){
}
public Department(String Name, int faculty){
this.DepartmentName = Name;
this.Status = faculty;
}
public Department(int id, String name, int faculty){
this.Id = id;
this.DepartmentName = name;
this.Status = faculty;
}
public int getId() {
return this.Id;
}
public void setId(int id) {
Id = id;
}
public String getDepartmentName() {
return this.DepartmentName;
}
public void setDepartmentName(String departmentName) {
DepartmentName = departmentName;
}
public int getStatus() {
return this.Status;
}
public void setFaculty(int faculty) {
Status = faculty;
}
public String toString(){
return DepartmentName;
}
}
主菜单活动
public class MainMenu extends AppCompatActivity {
DataBase dataBase = new DataBase(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
//create faculty
Faculty Science = new Faculty("Science");
long Science_id = dataBase.CreateFaculty(Science);
// create department
Department computer = new Department("Computer", 0);
Department maths = new Department("Maths",0);
Department EMT = new Department("Environmental Science",0);
Department physics = new Department("Physics",0);
long comp_id = dataBase.CreateDepartment(computer, new long[]{Science_id});
long math_id = dataBase.CreateDepartment(maths, new long[]{Science_id});
long emt_id = dataBase.CreateDepartment(EMT, new long[]{Science_id});
long physics_id = dataBase.CreateDepartment(physics, new long[]{Science_id});
Log.d("Sciences:", "Science Departments " + dataBase.GetDepartmentsBYFac("Science"));
// list of science departments
// final List<Department> sci_departments = dataBase.GetDepartmentsBYFac("Science");
ImageButton imageButton = (ImageButton) findViewById(R.id.science);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainMenu.this, DepartmentsActivity.class);
List<Department> departments = new ArrayList<Department>();
departments = dataBase.GetDepartmentsBYFac("Science");
Bundle bundle = new Bundle();
bundle.putSerializable("Sciences", (Serializable) departments);
intent.putExtra("Bundle", bundle);
startActivity(intent);
}
});
}
接收活动(listview)
public class DepartmentsActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_departments);
DataBase dataBase = new DataBase(this);
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("Bundle");
List<Department> departments = (ArrayList<Department>) bundle.getSerializable("Sciences");
ArrayAdapter<Department> adapter = new ArrayAdapter<Department>(this, android.R.layout.simple_list_item_1, departments);
setListAdapter(adapter);
}
}
listview结果显示
physics
physics
physics
physics
这是CreateDeppartment的方法
public long CreateDepartment(Department department, long[] fac_ids){
SQLiteDatabase database = this.getWritableDatabase();
values = new ContentValues();
values.put(KEY_DEPARTMENT, department. getDepartmentName());
values.put(KEY_STATUS, department.getStatus());
long dept_id =database.insert(TABLE_DEPARTMENT, null, values);
for (long fac_id: fac_ids) {
CreateDeptStatus(dept_id, fac_id);
}
return dept__id;
}
这是警告
Unchecked cast: 'java.io.Serializable' to 'java.util.ArrayList<com.example.umar.album.Models.Department>' less... (Ctrl+F1)
Signals places where an unchecked warning is issued by the compiler, for example:
void f(HashMap map) {
map.put("key", "value");
}
Hint: Pass -Xlint:unchecked to javac to get more details.