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

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

本文介绍了如何知道从可扩展列表视图中选中了哪个复选框并获取其信息(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发适用于Android的应用程序,但遇到了麻烦,我已经列出了一个可扩展列表,其中其子级为复选框.此活动从sql数据库获取其信息,唯一的问题是,按下确认按钮后,我不知道如何获取选中的复选框.

I'm working on an app for Android and I'm stuck, I already made a expandable list where its children are checkboxes. This activity gets its info from a sql database the only problem is that I don't know how to get which checkboxes were selected after the confirm button is pushed.

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

I mean all I want is to get the text or id of every checkbox that was checked so I can make an array and then put that information on a table in the database.

这是我的活动:

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

        }


    }




}



这是我的可扩展列表活动

And here is my expandable list 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;
    }


}

推荐答案

您可以将复选框值保留在哈希图中.

You can hold the checkbox values in a hashmap.

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

在您的getChildView函数中,在复选框上设置一个CheckedChange侦听器,并将复选框标签及其值放入哈希图中.

In your getChildView function set a CheckedChange listener on the checkbox and put the check box tag and its value to the 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:13