我在ExpandableListView
中有一个包含2个孩子的组,我想为点击的孩子的名字设置组名。
如何访问TextView
方法生成的getGroupView()
?
顺便说一句,如果可以用更好的方式编写,我会很乐意听到有关代码的一些提示
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
答案 0 :(得分:1)
1:请勿将数据放入适配器中。它属于您的expandableListView或在string.xml
中的Activity2:要刷新expandableListView,请将包含刷新数据的新适配器设置为ListView。
您的活动:
grupa = "Płeć"; //declare as String
dzieci = {"Kobieta","Mężczyzna"}; //declare as String[]
customAdapter = new CustomAdapter (this, grupa, dzieci);
expandableListView.setAdapter(customAdapter);
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();
grupa = dzieci[position];
customAdapter = new CustomAdapter (yourApplication.this, grupa, dzieci);
expandableListView.setAdapter(customAdapter);
return false;
}
});
您的适配器:
private Context context;
private String grupa;
private String[] dzieci;
public CustomAdapter(Context context, String groupNames, String[] childNames)
{
this.context = context;
this.grupa = groupNames;
this.dzieci = childNames;
}
//region gettersFolded
@Override
public int getGroupCount() {
return 1;
}
@Override
public int getChildrenCount(int position) {
return dzieci.length;
}
@Override
public Object getGroup(int groupPosition) {
return grupa;
}
@Override
public Object getChild(int group, int position) {
return dzieci[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);
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[childPosition]);
textView.setTextSize(12);
textView.setPadding(10,10,10,10);
return textView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
如果要使用多个组,则应尝试使用HashMaps而不是多维数组。 也许这个视频有帮助: https://www.youtube.com/watch?v=jZxZIFnJ9jE