gpt4 book ai didi

java - Android - 在 ExpandableListView 的每一行中的项目上设置监听器

转载 作者:行者123 更新时间:2023-12-01 18:34:29 24 4
gpt4 key购买 nike

在每一行上我有两个 EditText。当我更改第一个时,我希望另一个也被更改(如果需要,现在只需设置“25”进行测试)。我正在使用 View 持有者模式,并在每个第一个编辑文本上设置一个 TextChangedListener,期望第二个编辑文本也被更改。问题是,每当我更改任何行上的任何第一个编辑文本时,只有最后一行上的编辑文本被更改。这是适配器的代码(监听器位于 getgroupview 中):

public class CustomExpandableListAdapterNewWorkout extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;

private ChildViewHolder childViewHolder;
private GroupViewHolder groupViewHolder;

public CustomExpandableListAdapterNewWorkout(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final String expandedListText = (String) getChild(listPosition, expandedListPosition);
String categoryName= (String)getGroup(listPosition);

if (convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item_exercises_exercise, null);

childViewHolder = new ChildViewHolder();
childViewHolder.mChildTitle = (TextView) convertView.findViewById(R.id.expandedListItem);
childViewHolder.mChildImage = (ImageView) convertView.findViewById(R.id.ImgExercisePic);

convertView.setTag(childViewHolder);
}
else
{
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.mChildTitle.setText(expandedListText);
childViewHolder.mChildTitle.setTextAppearance(context,R.style.TitleStyle);




return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}

@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}

@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}

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

@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

String listTitle = (String) getGroup(listPosition);

if (convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group_new_workout_exercise, null);

groupViewHolder = new GroupViewHolder();
groupViewHolder.mGroupTitle = (TextView) convertView.findViewById(R.id.exerciseName);
groupViewHolder.mMinSets = (EditText) convertView.findViewById(R.id.edtMinimumSets);
groupViewHolder.mMaxSets = (EditText) convertView.findViewById(R.id.edtMaxSets);

groupViewHolder.mMinSets.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
groupViewHolder.mMaxSets.setText("25");
}

});

convertView.setTag(groupViewHolder);
}
else
{
groupViewHolder = (GroupViewHolder) convertView.getTag();
}

groupViewHolder.mGroupTitle.setText(listTitle);

return convertView;
}

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

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}

public final class GroupViewHolder {
TextView mGroupTitle;
EditText mMinSets;
EditText mMaxSets;
}

public final class ChildViewHolder {
TextView mChildTitle;
ImageView mChildImage;

}
}

可能有一些关于适配器和 View 持有者的基本知识我不明白,我想知道解决它的正确方法。

最佳答案

好吧,我的错。应该在 getGroupView 方法中声明“GroupViewHolder groupViewHolder”,这样每次都会创建一个具有新监听器的新监听器并影响其相应的行

关于java - Android - 在 ExpandableListView 的每一行中的项目上设置监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60085634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com