gpt4 book ai didi

android - 在 ExpandableListActivity 中处理 child

转载 作者:太空狗 更新时间:2023-10-29 16:10:02 25 4
gpt4 key购买 nike

我正在使用可扩展列表,如下所示: alt text

Expandable list Activity 有一个名为“OnGroupExpand”的方法,它允许您在父项展开和它的子项暴露之前执行一些工作。

在我的例子中,我想为每个 child 的复选框附加一个 checkChangeHandler,这样当它被点击时,我可以将 child 标记为完成。我显示了列表,当父级展开时子级显示,但我的复选框没有任何作用。

然后,我尝试在“onGroupExpand”方法中动态添加复选框处理程序。但是,当 onGroupExpand 被触发时,我在尝试获取对每个 subview 的引用时遇到非法转换异常

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

我的主要问题是获取对 subview 的引用,以便我可以动态附加处理程序。请查看我正在尝试执行此操作的代码注释。感谢您的宝贵时间...

最佳答案

我认为问题在于

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

不返回 View 但返回该位置的数据。您可能正在使用字符串或首选项对象或其他东西。您将获得它的基础对象,您可以对该对象进行更改。在下面的方法中创建 View 时(这由 ListView 自动调用),您可以设置任何 onCheckChanged 监听器:

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

添加示例代码

这是执行您想要执行的操作的示例适配器。 View 是在适配器上创建的,而不是在 Activity 上创建的。我假设您正在 Activity 中创建一个 SimpleExpandableListAdapter。取而代之的是,添加以下类并创建该 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;
}
}

关于android - 在 ExpandableListActivity 中处理 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4741671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com