在 onChildClickListener、ExpandableListView 更改组名

changing groupName at onChildClickListener, ExpandableListView

我在 ExpandableListView 中有一个包含 2 children 的组,我想为 child clicked 的名称设置组名称。

如何访问在 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

1:不要将数据放入适配器中。它属于 Activity 和您的 expandableListView 或 string.xml

2:要刷新您的 expandableListView,请将具有刷新数据的新适配器设置到您的 ListView。

你的Activity:

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

如果您想使用多个组,您应该尝试使用 HashMap 而不是多维数组。 也许这个视频有帮助: https://www.youtube.com/watch?v=jZxZIFnJ9jE