Android 带有复选框的自定义listview

Android 带有复选框的自定义listview,android,checkbox,custom-lists,Android,Checkbox,Custom Lists,大家好:)我有个问题 我做了一个带有复选框的自定义列表视图。我想知道当我单击listview复选框时是谁,我这样做了: prodottiList = new ArrayAdapter<String>(this, R.layout.prodotti_row, R.id.maggiore, prodottoArray) { public View getView(int position, View convertView, ViewGroup parent) {

大家好:)我有个问题

我做了一个带有复选框的自定义列表视图。我想知道当我单击listview复选框时是谁,我这样做了:

prodottiList = new ArrayAdapter<String>(this, R.layout.prodotti_row, R.id.maggiore, prodottoArray) {


        public View getView(int position, View convertView, ViewGroup parent) {

            View adapterView = super.getView(position, convertView, parent);

            String item = prodottoArray.get(position);

            currentCheckBox = (CheckBox)adapterView.findViewById(R.id.checkBox);

            Cursor c2 = myDb.rawQuery("SELECT * FROM selezionati WHERE prodotto='"+item+"'", null);
            int selezionatoId = c2.getColumnIndex("prodotto");
            while(c2.moveToNext()){
                  String selezionato = c2.getString(selezionatoId);

                  currentCheckBox.setChecked(true);
            }
            c2.close();


            listView.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long id) {

                    CheckBox currentCheckBoxSel = (CheckBox)arg1.findViewById(R.id.checkBox);



                    String valore = prodottoArray.get((int) id);
                    myDb.execSQL("CREATE TABLE IF NOT EXISTS selezionati (id VARCHAR, prodotto VARCHAR, categoria VARCHAR, lista VARCHAR);");
                    Cursor c = myDb.rawQuery("SELECT * FROM selezionati WHERE prodotto='"+valore+"' AND lista='"+nomeListaApostrofo+"' ", null);

                    int conteggio = c.getCount();
                    if(conteggio == 0){
                        currentCheckBoxSel.setChecked(true);
                        myDb.execSQL("INSERT INTO selezionati VALUES ('"+id+"','"+ valore +"','"+ categoria +"','"+ nomeListaApostrofo +"');");

                    }else{
                        currentCheckBoxSel.setChecked(false);
                        myDb.execSQL("DELETE FROM selezionati WHERE prodotto='"+ valore +"' AND lista='"+nomeListaApostrofo+"' AND categoria='"+categoria+"'  ;");
                        myDb.execSQL("DELETE FROM table_"+nomeListaEdit+" WHERE prodotto='"+ valore +"' ;");
                        myDb.execSQL("DELETE FROM completati WHERE prodotto='"+ valore +"' AND lista='"+nomeListaApostrofo+"' ;");

                    }
                    c.close();
                }

            });             

            return adapterView;
        }
prodottiList=new ArrayAdapter(这个,R.layout.prodotti_行,R.id.maggiore,prodottoArray){
公共视图getView(int位置、视图转换视图、视图组父视图){
View adapterView=super.getView(位置、转换视图、父级);
String item=prodottoArray.get(位置);
currentCheckBox=(复选框)adapterView.findViewById(R.id.CheckBox);
游标c2=myDb.rawQuery(“从selezionati中选择*,其中prodotto='”+item+“'”,null);
int selezionatoId=c2.getColumnIndex(“prodotto”);
while(c2.moveToNext()){
字符串selezionato=c2.getString(selezionatoId);
currentCheckBox.setChecked(真);
}
c2.关闭();
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(适配器视图arg0、视图arg1、内部位置、长id){
复选框currentCheckBoxSel=(复选框)arg1.findViewById(R.id.CheckBox);
字符串valore=prodottoArray.get((int)id);
execSQL(“如果不存在,则创建表selezionati(id VARCHAR、prodotto VARCHAR、categoria VARCHAR、lista VARCHAR);”;
游标c=myDb.rawQuery(“从selezionati中选择*,其中prodotto='”+valore+“'和lista='”+nomeListaApostrofo+”,null);
int conteggio=c.getCount();
if(conteggio==0){
currentCheckBoxSel.setChecked(真);
myDb.execSQL(“插入到selezionati值(“+id+”、“+valore+”、“+categoria+”、“+nomeListaApostrofo+”);”;
}否则{
currentCheckBoxSel.setChecked(假);
myDb.execSQL(“从selezionati中删除,其中prodotto='”+valore+“'和lista='”+nomeListaApostrofo+“'和categoria='“+categoria+”;”);
myDb.execSQL(“从表中删除”+nomeListaEdit+,其中prodotto='“+valore+”;”;
myDb.execSQL(“从completati中删除,其中prodotto='”+valore+“'和lista='”+nomeListaApostrofo+“;”);
}
c、 close();
}
});             
返回适配器视图;
}

但是,如果我单击第一个复选框,则打开第一个和最后一个复选框。如果我单击第二个复选框,则打开第二个和倒数第二个复选框??为什么?非常感谢您并为英文版感到抱歉

您好,这是我们使用自定义listview时的常见问题。这是因为listview很懒,并且使用了其他视图。这就是为什么它的chekbox是ge当你滚动列表时,点击。解决方法是使用listview的ViewHolder概念。你只需搜索ViewHolder概念,就会得到使用ViewHolder解释listview的站点数量

您当前的解决方案位于此链接上:


希望有帮助。

我尝试使用ViewHolder,但我发现这很困难。所以我尝试了这个系统,它看起来非常简单和有用。你确信没有修复程序吗?@Smile…我会建议你使用ViewHolder,它看起来很复杂,但它不复杂。我最初也考虑过它很复杂,但现在它是小菜一碟。它很容易尝试。它可以当您想在listview中保留editText的值时,它也很有用。