Thursday, October 20, 2011

Custom ListView with CheckBox

Hi, Layout: main.xml
<Relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/main_btn_data"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignparentbottom="true"
        android:layout_alignparentleft="true"
        android:onclick="onClickData"
        android:text="@string/hello" />
    <Listview
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/main_btn_data"
        android:layout_alignparentleft="true"
        android:layout_alignparenttop="true" />
</Relativelayout>

<Relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Textview
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignbaseline="@+id/checkBox1"
        android:layout_alignbottom="@+id/checkBox1"
        android:layout_alignparentleft="true"
        android:text="TextView"
        android:textappearance="?android:attr/textAppearanceLarge" />

    <Checkbox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignparentright="true"
        android:layout_alignparenttop="true" />

</Relativelayout>


MultipleCheckerActivity.java
public class MultipleCheckerActivity extends Activity {
 
 private ListView list;
 private List employee;
 private EmployeeAdapter adapter;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        employee = new ArrayList();
        fillData("Nishant", "Shah", 25, false);
        fillData("Chirag", "Shah", 22, false);
        fillData("Utsav", "Kapuriya", 22, false);
        
        list = (ListView) findViewById(R.id.listView1);
        adapter = new EmployeeAdapter(getApplicationContext(), R.layout.row_main, employee);
        list.setAdapter(adapter);
    }
    
    private void fillData ( String name, String lastName, int age, boolean isSelected ) {
     final EmployeeModel model = new EmployeeModel();
     model.setName(name);
     model.setLastName(lastName);
     model.setAge(age);
     model.setSelected(isSelected);
     
     employee.add(model);
    }
    
    public void onClickData ( View view ) {
     List empData = adapter.employeeData;
     System.out.println("Total Size :"  + empData.size());
     
     for (EmployeeModel employeeModel : empData) {
   if ( employeeModel.isSelected() ) {
    Log.e("Checker", employeeModel.getName());
   }
  }
    }
}


EmployeeAdapter.java
public class EmployeeAdapter extends BaseAdapter {
 
 public List employeeData;
 private Context mContext;
 private LayoutInflater mInflater;
 
 public EmployeeAdapter(Context context, int textViewResourceId,
   List objects) {
  this.employeeData = objects;
  this.mContext = context;
  mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }
 
 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  
  if (convertView == null) {
   holder = new ViewHolder();
   
   convertView = mInflater.inflate(R.layout.row_main, null);
   
   holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
   holder.chkTick = (CheckBox) convertView.findViewById(R.id.checkBox1);
   
   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }
  
  final int pos = position;
  holder.txtName.setText(employeeData.get(position).getName() + " " + 
    employeeData.get(position).getLastName());
  holder.chkTick.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    employeeData.get(pos).setSelected(isChecked);
   }
  });
  
  return convertView;
 }
 
 static class ViewHolder {
  TextView txtName;
  CheckBox chkTick;
 }

 public int getCount() {
  return employeeData.size();
 }

 public EmployeeModel getItem(int position) {
  return employeeData.get(position);
 }

 public long getItemId(int position) {
  return 0;
 }
}


EmployeeModel.java
public class EmployeeModel {

 private String name;
 private int age;
 private String lastName;
 private boolean selected;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public boolean isSelected() {
  return selected;
 }

 public void setSelected(boolean selected) {
  this.selected = selected;
 }
}

6 comments:

  1. Hi! Can you please demonstrate how we can get a list of all checked items? I would very much appreciate it. Thank you!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. thank you. it helped me a lot.
    and also, i have a dbt. can we use an array here instead of collection?

    ReplyDelete
  4. Great Job done by your end, it's very helpful for me, I want selection of checkBox on ListView Click, Is there any way.

    ReplyDelete
    Replies
    1. Naveen just use row of listview use View.OnClickListener then it will be work on any wehere you click on list will check on particular row.

      Delete