簡體   English   中英

android如何動態綁定復選框與自定義listview

[英]android how to dynamically bind checkboxes with custom listview

我動態創建復選框,我想將該復選框綁定到列表視圖。 我怎樣才能做到這一點?

在這里我給我代碼-

公共類HomeActivity擴展了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);

    db = new DBAdapter(HomeActivity.this);
    db.open();//int[] id=new int[]{Integer.parseInt(DBAdapter.ID)};

游標cr = db.getUncompletedTask(); //我的數據庫函數來檢索值以創建復選框,如果(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 []任務=新的String [] {DBAdapter.KEY_TODO};

    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);
            //lv1.addView(chk[i]);         
            //setContentView(lv1); 
        }

我在這里可以寫些什么,以便將此動態創建的復選框綁定到listview}

}

如果您使用帶復選框的android默認列表視圖怎么辦。 通過使用 :

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, COUNTRIES));

其中COUNTRIES是包含要顯示項目的靜態最終字符串數組。

您可以使用行包含CheckBox的自定義ListView。 創建擴展ArrayAdapter的自己的適配器,並使用覆蓋它的方法getView創建您的復選框。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM