自定义列表视图与复选框

时间:2012-01-06 19:15:06

标签: android checkbox custom-lists

大家好:)我有一个问题。

我使用复选框创建了自定义列表视图。我想要当我点击列表视图时打开复选框并执行此操作:

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;
        }

但如果我点击第一个复选框,请打开第一个和最后一个。如果我点击第二个和倒数第二个转弯?为什么?非常感谢你,对不起英语

1 个答案:

答案 0 :(得分:1)

嗨笑这是我们使用自定义列表视图时的常见问题。这是因为listview是懒惰的并且它使用其他视图..为什么chekbox被检查为sonn,因为你正在滚动列表。解决方案是tis是使用ViewHolder listview概念 .U只搜索viewHolder概念你将获得使用viewHolder解释listview的网站数量。

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

Storing the state of checkbox in listview while using custom BaseAdapter in android?

希望有所帮助......

相关问题