我在ExpandableListView中有一个包含2个孩子的组,我想为单击的孩子的名称设置组名称。
如何访问在getGroupView()方法中生成的TextView?
顺便说一下,如果能以更好的方式编写代码,我将很高兴听到一些关于代码的提示
CustomAdapter customAdapter = new CustomAdapter(this);
expandableListView.setAdapter(customAdapter);
expandableListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int group, int position, long id) {
String tekst = (String) parent.getExpandableListAdapter().getChild(group,position);
parent.collapseGroup(group);
Toast.makeText(getApplicationContext(), tekst ,Toast.LENGTH_SHORT).show();
return false;
}
});CustomAdapter.class:
private Context context;
public CustomAdapter(Context context)
{
this.context = context;
}
private String[] grupa = {"Płeć"};
private String[][] dzieci = {{"Kobieta","Mężczyzna"}};
//region gettersFolded
@Override
public int getGroupCount() {
return grupa.length;
}
@Override
public int getChildrenCount(int position) {
return dzieci[position].length;
}
@Override
public Object getGroup(int groupPosition) {
return grupa[groupPosition];
}
@Override
public Object getChild(int group, int position) {
return dzieci[group][position];
}
@Override
public long getGroupId(int group) {
return group;
}
@Override
public long getChildId(int group, int position) {
return position;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setText(grupa[groupPosition]);
textView.setPadding(10,10,10,10);
textView.setBackgroundResource(R.drawable.btm_line_shape);
textView.setTextSize(14);
return textView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View view, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setText(dzieci[groupPosition][childPosition]);
textView.setTextSize(12);
textView.setPadding(10,10,10,10);
return textView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
//endregion Regionhttps://stackoverflow.com/questions/47552879
复制相似问题