如何从可扩展列表视图中知道哪个复选框被选中并获取其信息(Android)

2023-12-25

我正在开发一个 Android 应用程序,但我陷入了困境,我已经制作了一个可扩展列表,其中它的子项是复选框。此活动从 SQL 数据库获取信息,唯一的问题是我不知道在按下确认按钮后如何获取选中了哪些复选框。

我的意思是,我想要的只是获取每个选中复选框的文本或 ID,这样我就可以创建一个数组,然后将该信息放在数据库的表中。

这是我的活动:

import java.util.ArrayList;


import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Point;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.Toast;

public class ManualQuestionnerActivity extends Activity implements OnClickListener{

    /*Variable de la Base de Datos*/
    protected DBHelper _db_helper;
    protected SQLiteDatabase _db;
    protected ExpandableListView _expandableList_seccion;
    protected Button _button_confirm;
    protected CheckBox _checkbox_child;
    int _id_company, _id_branch, _id_area,_id_subarea,_id_type,_id_questionner;
    String _string_timestarter;
    
    
    
    /**/
    private ExpandListAdapter _expandList_Adapter;
    private ArrayList<ExpandListGroup> _expandList_Items;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_questionner);
        
        Intent i = getIntent();

        /*inicializando las variables con valores de la base de datos*/
        _id_company = i.getIntExtra("company", 0);
        _id_branch = i.getIntExtra("branch", 0);
        _id_area = i.getIntExtra("area", 0);
        _id_subarea = i.getIntExtra("subarea", 0);
        _id_type = i.getIntExtra("type", 0);
        _id_questionner = i.getIntExtra("questionner", 0);
        _string_timestarter = i.getStringExtra("inicio");
        
        
        
        _expandableList_seccion = (ExpandableListView) findViewById(R.id.manual_questionner_expandablelistview);
        _button_confirm = (Button) findViewById(R.id.manual_questionner_button_confirm);
        
        _button_confirm.setOnClickListener(this);
        _checkbox_child = (CheckBox) findViewById(R.id.tvChild);
        //Log.w("LLEEEEEGOOOOOO", Integer.toString(seriesId));
        
        /*Creando la BD*/
        try
        {
            _db_helper = new DBHelper(getApplicationContext());
            _db_helper.createDataBase();
        }
        catch (Exception e)
        {
            Toast.makeText(this, "FAVOR DE CONTACTAR AL ADMINISTRADOR: error_#000", Toast.LENGTH_LONG).show();
        }
        
        /*Se abre la Base de Datos*/
        try 
        {
            _db_helper.openDataBase();
            _db = _db_helper.getReadableDatabase(); 
        }
        catch (Exception e) 
        {
            Toast.makeText(this, "FAVOR DE CONTACTAR AL ADMINISTRADOR: error_#001", Toast.LENGTH_LONG).show();
        }
        
        
        
     
        
        
        _expandList_Items = SetStandardGroups();//llenando la expandable list
        _expandList_Adapter = new ExpandListAdapter(ManualQuestionnerActivity.this, _expandList_Items);
        _expandableList_seccion.setAdapter(_expandList_Adapter);
        
        
        
        
    }
    
    public ArrayList<ExpandListGroup> SetStandardGroups() {
         
            final String [] ia ={"ia"};
            Cursor cursor_seccion = _db.query("i", ia, "ib = '" + _id_questionner + "'", null, null, null, null, null);
            cursor_seccion.moveToFirst();
            
            
            
            
                        
            
            
            ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
            
            
            
           
          
           do{
               
                ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();

                ExpandListGroup gru1 = new ExpandListGroup();

                gru1.setName(cursor_seccion.getString(0));
                /*temas*/
                final String [] _id = {"_id"};
                Cursor cursor_seccion_id = _db.query("i", _id, "ia = '"+cursor_seccion.getString(0)+"'", null,null,null,null,"1");
                cursor_seccion_id.moveToFirst();
                
                int value = Integer.parseInt(cursor_seccion_id.getString(0));
                
                /*preguntas*/
                final String[] ja = {"ja"};
                Cursor cursor_question = _db.query("j",ja,"jb = '"+value+"'",null,null,null,null,null);
                cursor_question.moveToFirst();
                
                
            
                
                Log.w("HOLAAAAAA",Integer.toString(cursor_question.getCount()));
                
                Log.w("HOLAAAAAA",cursor_question.getString(0));
                
                
                do
                {
                    ExpandListChild ch1_1 = new ExpandListChild();
                    ch1_1.setName(cursor_question.getString(0));//
                    ch1_1.setTag(null);
                    list2.add(ch1_1);

                    
                    
                }while(cursor_question.moveToNext());

                gru1.setItems(list2);

                
               
                
                list.add(gru1);
                
                
                
                
                
           }while(cursor_seccion.moveToNext());
            
            
            return list;
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.manual_questionner, menu);
        return true;
    }

    /*
    public void onCheckboxClicked(View view) 
    {
        if(_checkbox_child.isChecked() == true)
        {
        Log.w("SE PUSHOOOOO",":D");
        }
        else
        {
            Log.w("SE DESPUCHOOOOOOO","D:");
        }
    }
*/


    public void onClick(View v)
    {
        if(v.getId()== R.id.manual_questionner_button_confirm)
        {
            Toast.makeText(getApplicationContext(), "Guardando Datos.", Toast.LENGTH_SHORT).show();
            Intent home = new Intent(this, HomeActivity.class);
            startActivity(home);

        }
        
        
    }

    
    
    
}


    

这是我的可扩展列表活动

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<ExpandListGroup> grupos;
    
    public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> grupos)
    {
        this.context=context;
        this.grupos = grupos;
    }
    
    public void addItem(ExpandListChild item, ExpandListGroup group)
    {
        if(!grupos.contains(group))
        {
            grupos.add(group);
        }
        int index = grupos.indexOf(group);
        
        ArrayList<ExpandListChild> ch = grupos.get(index).getItems();
        ch.add(item);
        
        grupos.get(index).setItems(ch);
    }
    
    
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        
        ArrayList<ExpandListChild> chList = grupos.get(groupPosition).getItems();
        
        
        return chList.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        
        ExpandListChild child = (ExpandListChild) getChild(groupPosition,childPosition);
        
        if(view == null)
        {
            LayoutInflater infalInflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
             view = infalInflater.inflate(R.layout.expandlist_child_item, null);
        }
        
        CheckBox tv = (CheckBox) view.findViewById(R.id.tvChild);
        tv.setText(child.getName().toString());
        tv.setTag(child.getTag());

        return view;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        ArrayList<ExpandListChild> chList = grupos.get(groupPosition).getItems();
        return chList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
         return grupos.get(groupPosition);  
         }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return grupos.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        
         ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
         
         if(view == null)
         {
             LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
             view = inf.inflate(R.layout.expandlist_group_item, null);
         }
         
         TextView tv = (TextView) view.findViewById(R.id.tvGroup);
            tv.setText(group.getName());

        
        
        return view;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }
    enter code here
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }
    

}

您可以将复选框值保存在哈希图中。

public class ExpandListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private ArrayList<ExpandListGroup> grupos;
    HashMap<String, Boolean> mCheckBoxData = new HashMap<String, Boolean>();
        .....
}

在 getChildView 函数中,在复选框上设置一个 CheckedChange 侦听器,并将复选框标记及其值放入哈希映射中。

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {

    final ExpandListChild child = (ExpandListChild) getChild(groupPosition,childPosition);

    if(view == null){
        LayoutInflater infalInflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(R.layout.expandlist_child_item, null);
    }

    CheckBox checkBoxChild = (CheckBox)view.findViewById(R.id.checkbox_child);
    checkBoxChild.setText(child.getName().toString() );
    checkBoxChild.setTag(child.getTag());


    checkBoxChild.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean value) {
            mCheckBoxData.put(child.getTag(), value);
        }
    });

    return view;
}

// ExpandListAdapter's getCheckBoxData method 
public HashMap<String, Boolean> getCheckBoxData(){
    return mCheckBoxData;
}

确认按钮:

public void onClick(View v)
{
    if(v.getId()== R.id.manual_questionner_button_confirm)
    {
        Toast.makeText(getApplicationContext(), "Guardando Datos.", Toast.LENGTH_SHORT).show();
        //Intent home = new Intent(this, HomeActivity.class);
        //startActivity(home);
        HashMap<String, Boolean> checkBoxData = _expandList_Adapter.getCheckBoxData();

        // Get the keys  
        Set<String> keys = checkBoxData.keySet();  

        for (String key : keys) {
            Log.d("LOG", key + "=" + checkBoxData.get(key));  
        }  
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从可扩展列表视图中知道哪个复选框被选中并获取其信息(Android) 的相关文章

  • 如何忽略 LeakCanary 中的某些类?

    有人能给我一个如何忽略 LeakCanary 中的某些类的有效示例吗 我正在查看这个示例 以忽略 LeakCanary 中第三方库中的某些类 但我不知道将其放在应用程序中的何处 我把它放在我的应用程序类中 但这些变量和方法有错误 isInA
  • FLAG_ACTIVITY_REORDER_TO_FRONT 被忽略

    我有一个包含项目列表的 FragmentActivity 当应用程序处于后台时 可以推送该项目列表 发生这种情况时 我想创建一个状态栏通知并提醒用户更新 当用户单击通知时 活动应重新排序到前面并显示在屏幕上 同时在列表底部显示新项目 所以我
  • 多语言 Android 应用程序:在电子邮件和密码字段中显示英文键盘

    我们正在开发一款多语言 Android 应用程序 针对英语和阿拉伯语 面临的问题是在登录和注册屏幕中 我们希望仅以英文文本输入用户名和密码字段 从而显示英文键盘 无论设备区域设置语言如何 已尝试在 edittext 中设置 inputtyp
  • SQL中如何识别字符串的第一个字符是数字还是字符

    我需要将数据中的第一个字符识别为 SQL Server 中的数字或字符 我对此比较陌生 我不知道从哪里开始 但这是我到目前为止所做的事情 我的数据看起来像这样 TypeDep Transfer From 4Z2 Transfer From
  • 使用 RxJava 限制吞吐量

    我现在遇到的情况很难解释 所以我会写一个更简单的版本来解释这个问题 我有一个Observable from 它发出一系列由ArrayList文件数量 所有这些文件都应上传到服务器 为此 我有一个函数可以完成这项工作并返回一个Observab
  • Android BLE 扫描在后台几分钟后停止

    当我为公司开发新冠肺炎接触者追踪应用程序时 我在后台遇到了 Android 扫描停止问题 这是我尝试过的 添加前台服务 禁用手机中所有与电池相关的优化选项 启用后台运行的应用程序 测试设备 搭载 Android 10 的 Galaxy S2
  • 使用 mupdf android 库导航到特定页面

    我如何使用 muPDF 库导航到特定页面 或者有没有办法让图书馆不记得我最后在那个pdf文件中浏览的是哪一页 Uri uri Uri parse path Intent intent new Intent MainActivity getC
  • Android 消费品:“已经拥有该商品”,但 inventory.hasPurchase() 为 false

    我被 Google In App v3 困住了 我测试了一次没有消费的购买 例如 当应用程序在购买和消费之间崩溃时 现在我找不到出路 如果我尝试再次购买 它会显示 您已经拥有该商品 但是当我测试所有权时 它说我不拥有它 Inventory
  • 如何知道用户是否在 Android 应用程序中输入了错误的密码(锁定屏幕)

    我正在开发一个 Android 应用程序 如果用户在 Android 锁定屏幕中输入错误的密码 则必须完成其中一项活动 例如 如果用户输入错误的密码 则会发送电子邮件 我将不胜感激任何帮助 提前致谢 Kshitij 锁屏在完全沙箱环境中运行
  • 在 android 中,第一次单击时按钮侦听器未注册

    因为我是 Android 新手 所以我遇到了按钮监听器的问题 我正在使用 OnClickListener 来处理胸像 但它第一次点击后不执行一旦我单击多个 它就会表现良好 但如何使其在第一次单击时成为可能 这是我的代码 public cla
  • 使用后退按钮启动 Activity

    我正在 Android 中开发一个应用程序 我正在寻找解决方案 有一个活动 例如 A1 通过单击按钮 用户可以转到另一个活动 例如 A2 现在 一旦用户完成 A2 活动 他就会单击后退按钮 返回到上一个活动 A1 这是众所周知的事实 A1此
  • 无法登录 Google Play 游戏服务

    我在开发者控制台上使用包名称和正确的签名证书设置了我的游戏 并为其创建了排行榜 但没有创建任何成就 然后 我从以下位置下载了示例 Type A Number Challenge 和 BaseGameUtils https developer
  • 如何在 kotlin 中检查 lambda 空值

    在 Kotlin 中如何检查 lambda 是否为空 例如 我有这样的签名 onError Throwable gt Unit 我如何区分它的默认值是应用于主体还是应用于此函数的值 您无法测试 lambda 的主体是否为空 因此它不包含源代
  • Android 4.2 - Environment.getExternalStorageDirectory().getPath() 行为

    我一直在开发一个android应用程序 在上次更新到4 2之前 我使用 Environment getExternalStorageDirectory getPath 它返回了我 storage sdcard0 但自从更新后我现在得到了 s
  • MySQL 正在将我的时间戳值转换为 0000-00-00

    我是 PHP 新手 目前仍在学习中 我认为我的注册表有问题 username password email全部成功插入MySQL registered and last seen不要 我以为我正在使用getTimestamp 错了 但它呼应
  • 制作弹跳动画

    我想做图层的弹跳动画 我已经完成了该图层从右到中心的操作 现在我想将其向后移动一点 然后回到中心 这会产生反弹效果 我想我可以用这样的翻译来做到这一点
  • 使用“AND”表达式构建动态 SQL,而不混淆嵌套条件?

    总的来说 我对 php 和编码相当陌生 我有一系列条件需要测试它们是否已设置 它们是 option1 option2 option3 if isset option1 if isset option2 if isset option3 qu
  • 根据最大值连接表

    这是我正在谈论的内容的一个简化示例 Table students exam results id name id student id score date 1 Jim 1 1 73 8 1 09 2 Joe 2 1 67 9 2 09 3
  • MYSQL 按喜欢/不喜欢和受欢迎程度排序

    我有评论表 其中包括喜欢和不喜欢的内容 现在我在正确的顺序上遇到了问题 实际上 我的系统在顶部显示了最多点赞的评论 我正在 youtube 上寻找类似系统的东西 这意味着 100like 100dislikes 的评论的顺序高于 1 1 我
  • Entity Framework 6 多对多想要插入重复行

    不应该这么难 我准备放弃EF了 我的模型有周刊版本 每个版本可以有许多分类广告 每个分类可以出现在一个或多个版本中 我的模型 public class Classifieds Key DatabaseGenerated DatabaseGe

随机推荐