私はExpandableListView
に2人の子供を含む単一のグループを持っています。クリックした子の名前のグループ名を設定したいと思います。onChildClickListener、expandableListViewでgroupNameを変更する
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 Region