I had the same problem.Finally I solved with this code. Hope It will help you :)
//layout function
public void lay_out(List<String> exp1,int k){
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
container.removeAllViews();
for(int i=0;i<exp1.size();i++){
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView tv=(TextView)addView.findViewById(R.id.textView1);
final CheckBox cb=(CheckBox) addView.findViewById(R.id.checkbox1);
if(k==0)
{
cb.setChecked(false);
}
else
{
cb.setChecked(true);
}
tv.setText(name1.get(i));
cb.setText(phno1.get(i));
container.addView(addView);
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(),cb.getText().toString(),10).show();
if(cb.isChecked()==true){
if(checkedcontacts.length()<0)
{
for(int i=0;i<=checkedcontacts.length();i++)
{
if(!checkedcontacts.equals(cb.getText().toString()))
{
// insertion
checkedcontacts.append(cb.getText().toString());
checkedcontacts.append("\n");
}
}
}
else{
checkedcontacts.append(cb.getText().toString());
checkedcontacts.append("\n");
}
}
else if(cb.isChecked()==false){
s_all.setChecked(false);
try{
Toast.makeText(getApplicationContext(),cb.getText().toString(),10).show();
//Pattern.compile(cb.getText().toString()).matcher(checkedcontacts).replaceAll("");
checkedcontacts.delete(checkedcontacts.indexOf(cb.getText().toString()),
cb.getText().toString().length());
}
catch(Exception e){
System.out.println("error on array"+e);
}
}
}
});
container.setVisibility(View.VISIBLE);
}
}