【问题标题】:Custom listview with checkboxes带有复选框的自定义列表视图
【发布时间】:2012-01-07 03:28:17
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: android checkbox custom-lists


    【解决方案1】:

    嗨,微笑,这是我们使用自定义列表视图时的常见问题。发生这种情况是因为列表视图是惰性的,它使用其他视图。这就是为什么当你滚动列表时检查框被检查为 Sonn。解决方案是使用 listview 的 ViewHolder 概念。你只需搜索 viewHolder 概念,你将获得使用 viewHolder 解释 listview 的网站数量。

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

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

    希望对你有帮助..

    【讨论】:

    • 我尝试使用 ViewHolder 但我发现这非常困难。所以我尝试了这个系统,它看起来非常简单和有用。你是 sicure 不存在修复?
    • @Smile...我会推荐你​​使用 viewHolder,它看起来很复杂,但实际上并不复杂。我最初也认为它很复杂,但现在它是小菜一碟。它很容易尝试。它当您想在 listview 中保留 editText 的值时也会很有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多