android - 如何知道从可扩展 ListView 中选中了哪个复选框并获取其信息(Android)

标签 android sql checkbox expandablelistview

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

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

这是我的 Activity :

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

        }
        
        
    }

    
    
    
}


    

这是我的可扩展列表 Activity

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

}

最佳答案

您可以将复选框值保存在 HashMap 中。

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

在您的 getChildView 函数中,在复选框上设置一个 CheckedChange 监听器,并将复选框标记及其值放入 HashMap 中。

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

}

关于android - 如何知道从可扩展 ListView 中选中了哪个复选框并获取其信息(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16501933/

相关文章:

jquery - HTML 复选框的酷替代品

php - 如何向具有复选框的查询添加分页?

android - 单击按钮时从互联网播放声音文件

php - 困难的 SQL 查询语法

sql - Windows 服务还是 SQL 作业?

mysql - 使用一个 MySQL 表中的值更新另一个表

javascript - 如果选中复选框,则在 Facebook 墙上发布消息

java - Android中的画笔动画效果

android - 实现 Android 深度链接的最小 API

android - 在 onCreate() 中生成 main.xml