android可扩展列表视图从子onclick侦听器获取组视图
我在我的应用程序中有一个可扩展的列表视图,每个组都有一个文本视图,比如说T有动态值,每个组都有一个子组。我想引用该组的T textview对象,该组的子项已被单击,因此我可以根据单击的子项相应地更改T的值 如果是groupClickListener,我可以很容易地获取TextView对象,但是如果是childClickListener,我无法获取。。。请看这两个 mExpandableListView.setOnGroupClickListener(新OnGroupClickListener(){ setOnChildClickListener(新的OnChildClickListener()){android可扩展列表视图从子onclick侦听器获取组视图,android,textview,expandablelistview,Android,Textview,Expandablelistview,我在我的应用程序中有一个可扩展的列表视图,每个组都有一个文本视图,比如说T有动态值,每个组都有一个子组。我想引用该组的T textview对象,该组的子项已被单击,因此我可以根据单击的子项相应地更改T的值 如果是groupClickListener,我可以很容易地获取TextView对象,但是如果是childClickListener,我无法获取。。。请看这两个 mExpandableListView.setOnGroupClickListener(新OnGroupClickListener()
我希望它对您有用。使用“父级”上的
getExpandableListAdapter()
,并相应地定义自定义类。
然后修改自定义对象中的值-ExpandableListView将自动更新。
例如:
不太清楚为什么会发生这种情况-似乎ExpandableAdapter正在跟踪对模型本身的更改…我没有时间检查有关这方面的详细信息…但它可以工作。请显示您所做的工作。如何从中获取对TextView对象的引用,以及这里的listDataChild是什么。
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
TextView moreTextView = (TextView)v.findViewById(R.id.group_more); // this works fine
}
return false;
}
});
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
boolean isExpanded=false;
if(groupStatus[groupPosition]==1)
{
isExpanded=true;
}
View v1 = parent.getExpandableListAdapter().getGroupView(groupPosition, isExpanded, v, parent);
TextView moreTextView = (TextView)v1.findViewById(R.id.group_more); // doesn,t work as v1 is null all the times
return false;
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
@Override
public boolean onChildClick(ExpandableListView _parent, View _v,
int _groupPosition, int _childPosition, long _id
) {
// do required casts for your custom objects for groups and childs.
// In my case, I'm using class "Group"
Group group=(Group)parent.getExpandableListAdapter().getGroup(_groupPosition);
// In my case, I'm using class "Child"
Child item=(Child)optionsList.getExpandableListAdapter().getChild(_groupPosition, _childPosition);
// In my custom Group class, I've defined a field that populates a different
// TextView within the Group layout - so, getter/setter method for "selection" were defined.
// The ExpandableListView will be updated automatically based on the adapter updates.
group.setSelection(item.getName());
return true;
}