动态添加复选框只显示最后一个值(Dynamically Added Checkboxes Shows Only Last Value)
这是我的代码,只导致员工列表的最后一个值
public void addCheckBoxesView() { Employee ee; CheckBox chkbox; ArrayList<Employee> employee_list= initEmployees(); for(int i =0; i<employee_list.size();i++) { ee = (Employee) employee_list.get(i); chkbox = new CheckBox(this); chkbox.setId(ee.getID()); chkbox.setText(ee.getName()); layout.addView(chkbox); } }Here is my code that results in only last value of Employee List
public void addCheckBoxesView() { Employee ee; CheckBox chkbox; ArrayList<Employee> employee_list= initEmployees(); for(int i =0; i<employee_list.size();i++) { ee = (Employee) employee_list.get(i); chkbox = new CheckBox(this); chkbox.setId(ee.getID()); chkbox.setText(ee.getName()); layout.addView(chkbox); } }最满意答案
Try this
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_root" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout>MainActivity.xml
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.my_root); LinearLayout sublayout = new LinearLayout(this); sublayout.setOrientation(LinearLayout.HORIZONTAL); Employee ee; CheckBox chkbox; ArrayList<Employee> employee_list = new ArrayList<>(); Employee employee=new Employee(); employee.setId(1); employee.setName("as"); employee_list.add(employee); Employee bb=new Employee(); bb.setId(4); bb.setName("siva"); employee_list.add(bb); Employee hsud=new Employee(); hsud.setId(16); hsud.setName("kumar"); employee_list.add(hsud); for (int i = 0; i < employee_list.size(); i++) { ee = (Employee) employee_list.get(i); chkbox = new CheckBox(this); chkbox.setId(ee.getId()); chkbox.setText(ee.getName()); sublayout.addView(chkbox); } rootLayout.addView(sublayout);Employee.xml
class Employee { int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } String name; }Try this
Layout file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_root" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout>MainActivity.xml
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.my_root); LinearLayout sublayout = new LinearLayout(this); sublayout.setOrientation(LinearLayout.HORIZONTAL); Employee ee; CheckBox chkbox; ArrayList<Employee> employee_list = new ArrayList<>(); Employee employee=new Employee(); employee.setId(1); employee.setName("as"); employee_list.add(employee); Employee bb=new Employee(); bb.setId(4); bb.setName("siva"); employee_list.add(bb); Employee hsud=new Employee(); hsud.setId(16); hsud.setName("kumar"); employee_list.add(hsud); for (int i = 0; i < employee_list.size(); i++) { ee = (Employee) employee_list.get(i); chkbox = new CheckBox(this); chkbox.setId(ee.getId()); chkbox.setText(ee.getName()); sublayout.addView(chkbox); } rootLayout.addView(sublayout);Employee.xml
class Employee { int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } String name; }动态添加复选框只显示最后一个值(Dynamically Added Checkboxes Shows Only Last Value)这是我的代码,只导致员工列表的最后一个值
public void addCheckBoxesView() { Employee ee; CheckBox chkbox; ArrayList<Employee> employee_list= initEmployees(); for(int i =0; i<employee_list.size();i++) { ee = (Employee) employee_list.get(i); chkbox = new CheckBox(this); chkbox.setId(ee.getID()); chkbox.setText(ee.getName()); layout.addView(chkbox); } }Here is my code that results in only last value of Employee List
public void addCheckBoxesView() { Employee ee; CheckBox chkbox; ArrayList<Employee> employee_list= initEmployees(); for(int i =0; i<employee_list.size();i++) { ee = (Employee) employee_list.get(i); chkbox = new CheckBox(this); chkbox.setId(ee.getID()); chkbox.setText(ee.getName()); layout.addView(chkbox); } }最满意答案
Try this
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_root" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout>MainActivity.xml
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.my_root); LinearLayout sublayout = new LinearLayout(this); sublayout.setOrientation(LinearLayout.HORIZONTAL); Employee ee; CheckBox chkbox; ArrayList<Employee> employee_list = new ArrayList<>(); Employee employee=new Employee(); employee.setId(1); employee.setName("as"); employee_list.add(employee); Employee bb=new Employee(); bb.setId(4); bb.setName("siva"); employee_list.add(bb); Employee hsud=new Employee(); hsud.setId(16); hsud.setName("kumar"); employee_list.add(hsud); for (int i = 0; i < employee_list.size(); i++) { ee = (Employee) employee_list.get(i); chkbox = new CheckBox(this); chkbox.setId(ee.getId()); chkbox.setText(ee.getName()); sublayout.addView(chkbox); } rootLayout.addView(sublayout);Employee.xml
class Employee { int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } String name; }Try this
Layout file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_root" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout>MainActivity.xml
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.my_root); LinearLayout sublayout = new LinearLayout(this); sublayout.setOrientation(LinearLayout.HORIZONTAL); Employee ee; CheckBox chkbox; ArrayList<Employee> employee_list = new ArrayList<>(); Employee employee=new Employee(); employee.setId(1); employee.setName("as"); employee_list.add(employee); Employee bb=new Employee(); bb.setId(4); bb.setName("siva"); employee_list.add(bb); Employee hsud=new Employee(); hsud.setId(16); hsud.setName("kumar"); employee_list.add(hsud); for (int i = 0; i < employee_list.size(); i++) { ee = (Employee) employee_list.get(i); chkbox = new CheckBox(this); chkbox.setId(ee.getId()); chkbox.setText(ee.getName()); sublayout.addView(chkbox); } rootLayout.addView(sublayout);Employee.xml
class Employee { int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } String name; }
发布评论