Search code examples
androidtextviewexpandablelistviewexpandablelistadapter

TextView.setText() doesn't update text in ExpandableListView adapter


I wrote a class that extends BaseExpandableListAdapter. When a group is clicked, I want it to expand, and then my custom indicator in the view will animate a 180 rotation. And it works.

My problem is that on the same clicklistener I want to update a TextView using setText() method, but when I run it, it doesn't update the view.
I tested if the problem was in the reference to the TextView: changing the visibility, rotating it, animating it, and they all work. Its just the setText() that doesn't works.
Here is the relevant code in the adapter class: (There are actually two TextViews I would like to update)

@Override
public void onGroupExpand(int groupPosition) {
    GroupViewHolder holder = mViewHolderSparseArray.get(groupPosition);
    Lesson lesson = (Lesson) getChild(groupPosition, 0);
    holder.indicatorView.animate().rotation(180);
    holder.teacherView.setText(lesson.getTeacher());
    holder.classroomView.setText(lesson.getClassroom());
}

@Override
public void onGroupCollapse(int groupPosition) {
    GroupViewHolder holder = mViewHolderSparseArray.get(groupPosition);
    holder.indicatorView.animate().rotation(0);
    holder.teacherView.setText("...");
    holder.classroomView.setText("");
}

I have also tried to put this code in the getGroupView() (assign an OnClickListener to the view), but I get the same results.

Some other relevant methods:

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

Any ideas why my TextView doesn't update its text?


Solution

  • According to the answer linked by @IntelliJAmiya, The setText() doesn't update because it probably didn't run on the UI thread.

    Here Is what I did to fix this issue:

    @Override
    public void onGroupExpand(int groupPosition) {
        final GroupViewHolder holder = mViewHolderSparseArray.get(groupPosition);
        final Lesson lesson = (Lesson) getChild(groupPosition, 0);
        holder.indicatorView.animate().rotation(180);
        holder.teacherView.post(new Runnable() {
            @Override
            public void run() {
                holder.teacherView.setText(lesson.getTeacher());
                holder.classroomView.setText(lesson.getClassroom());
            }
        });
    }