android how to dynamically bind checkboxes with custom listview
I create checkboxes
dynamically & i want to bind that checkboxes
to listview
.
How can I do that?
Here i give my code--
public class HomeActivity extends ListActivity{
CheckBox[] chk;
ListView lv1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.listTasks);
tv1=(TextView) findViewById(R.id.tvMsg);
The database section:
db = new DBAdapter(HomeActivity.this);
db.open();//int[] id=new int[]{Integer.parseInt(DBAdapter.ID)};
Cursor cr=db.getUncompletedTask();//my database function to retrieve values to create checkboxes
if (cr.moveToFirst()) {
do {
String[] str=new String[2];
str[0]=cr.getString(0);
str[1]=cr.getString(1);
al.add(str);
} while (cr.moveToNext());
}
startManagingCursor(cr);
String[] tasks = new String[] { DBAdapter.KEY_TODO };
Creation of checkbox:
int[] idchk=new int[al.size()];//here i am creating checkbox dynamicaly
if (al.size() != 0) {
chk = new CheckBox[al.size()];
System.out.println(al.size());
for (int i = 0; i < al.size(); i++) {
String[] s = (String[]) al.get(i);
System.out.println("ID: "+s[0]);
Task_Id = Integer.parseInt(s[0]);
Task_Nm = s[1];
chk[i] = new CheckBox(HomeActivity.this);
System.out.println(i +"task id"+Task_Id +"parseint"+Integer.parseInt(s[0]+chk[i].getText().toString()));
chk[i].setId(Task_Id);
idchk[i]=Task_Id;
chk[i].setText(Task_Nm);
开发者_StackOverflow社区 //lv1.addView(chk[i]);
//setContentView(lv1);
}}}
Here what can i write here so that this dynamically created checkboxes
will be bind to listview
What if you use the default listview of android with checkbox. By using :
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, COUNTRIES));
where COUNTRIES is static final string array that contains the item to show..
You can use customized ListView whose rows contains CheckBox. Create your own adapter extending ArrayAdapter and it's overridden method getView create your checkboxes.
精彩评论