在ExpandableListActivity儿童工作 [英] Working with children in ExpandableListActivity

查看:128
本文介绍了在ExpandableListActivity儿童工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与可扩展列表工作,如下所示:

I'm working with an Expandable List, like this:

在扩展列表活动有一个名为OnGroupExpand的方法,让你的父母,并扩大它的儿童接触之前执行一些工作。

The Expandable list activity has a method called "OnGroupExpand" which allows you to perform some work before the parent expands and it's children are exposed.

在我的情况,我想一个checkChangeHandler重视每个孩子的复选框,以便单击时,我可以标记孩子为完成。我有列表显示出来,孩子们表明,当家长的扩展,但我的复选框什么都不做。

In my case, I want to attach a checkChangeHandler to each child's checkbox so that when it's clicked, I can mark the child as complete. I have the list showing up and the children show when the parent expands, but my check box does nothing.

然后,我尝试在onGroupExpand的方式动态地添加复选框处理程序。然而,onGroupExpand时触发的我得到一个非法转换异常当试图让每个孩子视图的引用。

Then, I try to add checkbox handlers dynamically in the "onGroupExpand" method. However, when onGroupExpand is triggered I get an illegal cast exception when trying to get a reference to each child view.

public void  onGroupExpand (int groupPosition) {

      int numOfChildren = expListAdapter.getChildrenCount(groupPosition);


            for(int i = 0; i < numOfChildren; i++)
            {
                 //Get exception here because getChild() returns Object - but how else can
                 //can I attach a check box to each child?
                 View v = (View) expListAdapter.getChild(groupPosition, i);


                 CheckBox cb = (CheckBox)v.findViewById( R.id.checkComplete ); 
                 cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
                    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                        Toast.makeText(getBaseContext(), "Check Changed for " + groupPosition, 2000);
                    }
                  });
             }

我的主要问题是关于让一个参考子视图,这样我可以动态附加的处理程序。请看到我试图做到这一点,code评论。
感谢您的时间...

My primary question is about getting a reference to the Child view so that I can attach a handler dynamically. Please see the code comment where I'm trying to do this. Thanks for your time...

推荐答案

我相信问题是,

View v = (View) expListAdapter.getChild(groupPosition, i);

不返回查看但返回的数据该位置。你可能会使用字符串或preFS对象或别的东西。你会得到你的潜在对象,你可以修改该对象。当在下面的方法创建的视图(这是由ListView控件自动调用),你可以设置任何onCheckChanged监听器:

Doesn't return a View but is returning the data for that position. You might be using strings or a prefs object or something else. You will get your underlying object for it, you can make changes to that object. When the view is created in the method below (and this is called automatically by the ListView), you can setup any onCheckChanged listeners:

abstract View    getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)

添加例如code

下面是例子适配器,做你想做的事。的意见是对适配器,而不是活动创建。我假设你正在创建你的活动SimpleExpandableListAdapter。替代的是,添加以下类,并创建一个视图。

Here is the example Adapter that does what you wanted to do. The Views are created on the Adapter and not the Activity. I assume you are creating a SimpleExpandableListAdapter in your Activity. Instead of that, add the following class and create that view.

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ExampleAdapter extends SimpleExpandableListAdapter {

    protected Context mContext;

    public ExampleAdapter(ExampleActivity exampleActivity, List createGroupList, int groupRow, String[] strings, int[] is, List createChildList, int childRow, String[] strings2, int[] is2) {
        super(exampleActivity, createGroupList, groupRow, strings, is, createChildList, childRow, strings2, is2);
        mContext = exampleActivity;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);

        if (v != null) {
            final String value = "Check Changed for " + groupPosition + " " + childPosition;
            CheckBox cb = (CheckBox)v.findViewById( R.id.checkComplete ); 
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
               public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                   Toast toast = Toast.makeText(mContext, value, 2000);
                   toast.show();
               }
             });
        }

        return v;
    }    
}

这篇关于在ExpandableListActivity儿童工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆