Android-在ExpandableListView每行中的项目上设置侦听器

问题描述 投票:0回答:1

每行上都有两个EditTexts。当我更改第一个EditTexts时,我也希望更改另一个EditTexts(如果需要,现在只需在此处设置'25'进行测试)。我使用的是Viewholder模式,并且在每个第一个edittext上设置一个TextChangedListener,期望第二个也要更改。问题是,每当我更改任何一行中的任何一个第一个edittext时,仅最后一行上的edittext都会更改。这是适配器的代码(侦听器在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;

    }
}

可能有一些关于适配器和视图持有人的基本知识,我想知道解决该问题的正确方法。

java android listview listener
1个回答
0
投票

好,我的坏。应该在getGroupView方法中声明“ GroupViewHolder groupViewHolder”,这样每次都会创建一个带有新侦听器的新对象,并影响其对应的行。

© www.soinside.com 2019 - 2024. All rights reserved.