【问题标题】:Android get value of checkbox from listviewAndroid从列表视图中获取复选框的值
【发布时间】:2015-11-05 01:30:40
【问题描述】:

我有这段代码,我需要删除在列表视图中检查的数据,但我的问题是我只得到第一个复选框。当我选中第二个复选框时,它没有检索到数据。 getChildCount() 也返回 0 为什么会这样?我不知道我将如何解决我的问题。谢谢。

这是我的代码:

btnDel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RetailerDatabaseHelper dbHelper = new RetailerDatabaseHelper(TemplateActivity.this);
            final SQLiteDatabase db = dbHelper.getReadableDatabase();
            CheckBox cb = (CheckBox) findViewById(R.id.checkBox1); //For further investigation!

            Cursor c10 = db.query("Retailer", new String[]{"_id", "name", "validity"}, null, null, null, null, null, null);
            ListAdapter adapter = new SimpleCursorAdapter(TemplateActivity.this, R.layout.custom_dialog_box, c10, new String[]{"name", "validity"}, new int[]{R.id.checkBox1, R.id.number}, 0);
            ListView myList = (ListView) findViewById(R.id.listview);
            myList.setAdapter(adapter);

            Log.d("Inside Button", "Test Child: " + myList.getChildCount() + " Adapter: " + adapter.getCount() + " getAdapter: " + myList.getAdapter().getCount() + " Child: " + myList.getChildAt(1));

            for(int x = 0; x < myList.getAdapter().getCount(); x++)
            {
                View nextChild = myList.getChildAt(x);
                if(nextChild instanceof CheckBox)
                {
                    CheckBox checkbox = (CheckBox)nextChild;
                    if(checkbox.isChecked())
                    {
                        Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    });

【问题讨论】:

    标签: java android android-sqlite


    【解决方案1】:

    我会先说:永远不要在 Android 的 UI/主线程上进行数据库工作。它将阻塞 UI 并导致您的应用程序冻结。查看this resource for more info 的“线程”部分。

    要解决您当前的问题,您可能需要维护一个“已检查”项目的列表。这可能涉及维护已使用 a listener on your check boxes 填充的 id 列表,您可以在列表的适配器视图绑定代码中设置该列表。在检查更改后,您可以将 id 添加到您的列表中,并在删除时告诉您的数据库需要删除哪些 id。对于此解决方案,您可能需要研究制作自己的适配器。简单的光标适配器不会让你走得太远;)

    另外,您可能想查看 RecyclerView - ListView 的较新版本。

    【讨论】:

    • 哦。抱歉,我是 android 编程新手,这就是为什么我不知道自己的方式。
    【解决方案2】:

    @Kathleen51
    我在 BaseAdapter 中使用 SparseBooleanArray。像这样编辑您的代码...

    class OutPatientServiceAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private ArrayList<OutPatientService> beans;
        private viewHolder holder;
        private Context c;
        private SparseBooleanArray booleanArray;
    
        public OutPatientServiceAdapter(Context c,
                ArrayList<OutPatientService> beans) {
            super();
            this.c = c;
            this.beans = beans;
            inflater = LayoutInflater.from(c);
            booleanArray = new SparseBooleanArray(beans.size());
            for (int i = 0; i < beans.size(); i++) {
                booleanArray.put(i, true);
            }
        }
    
        @Override
        public int getCount() {
            return beans.size();
        }
    
        @Override
        public OutPatientService  getItem(int position) {
            return beans.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            holder = new viewHolder();
            final OutPatientService bean = (OutPatientService) getItem(position);
            if (convertView == null) {
                convertView = inflater.inflate(
                        R.layout.outpatient_service_item, parent, false);
                holder.itemText = (CheckBox) convertView
                        .findViewById(R.id.itemText);
                holder.doctorText = (TextView) convertView
                        .findViewById(R.id.doctorText);
                holder.priceText = (TextView) convertView
                        .findViewById(R.id.priceText);
                convertView.setTag(holder);
            } else {
                holder = (viewHolder) convertView.getTag();
            }
            boolean check = booleanArray.get(position);
            holder.itemText.setChecked(check);
            holder.itemText
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            booleanArray.put(position, isChecked);
                        }
                    });
            holder.itemText.setText(bean.recipe_name);
            holder.doctorText.setText(bean.create_doctor);
            holder.priceText.setText(bean.fee);
            return convertView;
        }
    
        public class viewHolder {
            public CheckBox itemText;
            public TextView doctorText;
            public TextView priceText;
        }
    
        public SparseBooleanArray getmSpCheckedState() {
            return booleanArray;
        }
    
        public void selectAll() {
            for (int i = 0; i < beans.size(); i++) {
                if (!booleanArray.get(i)) {
                    booleanArray.put(i, true);
                }
            }
            notifyDataSetChanged();
        }
    
        public void clearAll() {
            for (int i = 0; i < beans.size(); i++) {
                if (booleanArray.get(i)) {
                    booleanArray.put(i, false);
                }
            }
            notifyDataSetChanged();
        }
    }
    

    【讨论】:

    • 你好,如果数据来自数据库怎么办?
    • 不要使用 SimpleCursorAdapter。您可以创建包含 id、name、validity 的自定义对象。然后将 db.query 转换为 ArrayList.
    • 你能告诉我我应该如何处理自定义对象吗?
    • 创建一个类。公共类 OutPatientService { public int id;公共字符串名称;公共字符串有效性; }
    猜你喜欢
    • 2017-04-18
    • 1970-01-01
    • 2016-02-13
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2013-02-24
    • 1970-01-01
    相关资源
    最近更新 更多