Question

Hello everyone :) I have a problem.

I did a custom listview with a checkboxes. I want who when I click the listview the checkbox turn on and I did this:

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

But if I click on the first checkboxes turn on the first and the last. If I click the second turn on the second and the penultimate ?.? Why ? Thank You so much and sorry for the English

Was it helpful?

Solution

Hi Smile this is the common problem when we use custom listview .This happens as listview is lazy and it uses others view ..thats why it the chekbox is getting checked as sonn as u are scrolling the list .Solution to tis is to use ViewHolder concept of listview .U just search for viewHolder concept u will get number of sites explaining listview using viewHolder.

Your current solution is on this link:

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

Hope it helps..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top