Dynamically Added Checkboxes Shows Only Last Value
Here is my code that results in only last value of Employee List public void addCheckBoxesView() { Employee ee; CheckBox chkbox; ArrayList
Solution 1:
Try this
Layout file
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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
classEmployee {
int id;
public int getId() {
return id;
}
publicvoidsetId(int id) {
this.id = id;
}
publicStringgetName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
String name;
}
Solution 2:
Try below if it works.
publicvoidaddCheckBoxesView()
{
ArrayList<Employee> employee_list= initEmployees();
for(Employee ee : employee_list)
{
CheckBox chkbox = new CheckBox(this);
chkbox.setId(ee.getID());
chkbox.setText(ee.getName());
layout.addView(chkbox);
}
}
Post a Comment for "Dynamically Added Checkboxes Shows Only Last Value"