可扩展列表视图重复的子项

时间:2016-08-24 16:03:18

标签: android android-layout listview

问题是,当我点击任何可展开的列表视图标题时,所有其他标题及其子标题将变为已按下的标题,我不明白为什么!?

我的想法是我有一个地址列表。每个标题都是地址行。正文由3个EditTexts组成:地址行,apt,邮政编码。标题和正文的地址行是相同的,当主体地址行改变时,它应该反映标题中的地址行。

public class AddressExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private ArrayList<Address> mAddresses;

    public AddressExpandableListAdapter(Context contextIn) {
        mContext = contextIn;
        mAddresses = (MapHelper.getInstance()).getAddressList();
    }

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

    @Override
    public int getChildrenCount(int i) {
        return 1;
    }

    @Override
    public Object getGroup(int i) {
        return mAddresses.get(i);
    }

    @Override
    public Object getChild(int i, int j) {
        return mAddresses.get(i);
    }

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

    @Override
    public long getChildId(int i, int j) {
        return i;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.addresslist_header, null);
        }

        TextView header = (TextView) view
                .findViewById(R.id.addresslist_header_title);
        header.setText(((Address) getGroup(i)).getAddressLine(0));

        return view;
    }

    @Override
    public View getChildView(int i, int j, boolean b, View view, ViewGroup viewGroup) {

        ViewHolder holder;
        Address data = (Address) getChild(i,j);
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.addresslist_body, null);
            holder = new ViewHolder(view, i);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.setData(data);
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private class ViewHolder {
        EditText addressLine;
        EditText apt;
        EditText postalCode;
        int position;

        ViewHolder(View viewIn, int i) {

            addressLine = (EditText) viewIn
                    .findViewById(R.id.addresslist_body_addressline);
            apt = (EditText) viewIn
                    .findViewById(R.id.addresslist_body_apt);
            postalCode = (EditText) viewIn
                    .findViewById(R.id.addresslist_body_postalcode);
            position = i;

            setAddressLineWatcher(addressLine);
            setAptWatcher(apt);
            setPostalCodeWatcher(postalCode);
        }

        private void setData(Address data) {
            addressLine.setText(data.getAddressLine(0));
            postalCode.setText(data.getPostalCode());
        }

        private void setAddressLineWatcher(final EditText addressLineIn) {
            TextWatcher textWatcher = new TextWatcher() {
                boolean changed = false;

                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if (!changed) {
                        changed = true;
                        addressLineIn.setText(charSequence.toString());
                        updateAddressLine(charSequence.toString(), position);

                    }
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    changed = false;
                }
            };
            addressLineIn.addTextChangedListener(textWatcher);
        }

        private void setAptWatcher(final EditText aptIn) {
            TextWatcher textWatcher = new TextWatcher() {
                boolean changed = false;

                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if (!changed) {
                        changed = true;
                        aptIn.setText(charSequence.toString());
                    }
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    changed = false;
                }
            };

            aptIn.addTextChangedListener(textWatcher);
        }

        private void setPostalCodeWatcher(final EditText postalCodeIn) {
            TextWatcher textWatcher = new TextWatcher() {
                boolean changed = false;

                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if (!changed) {
                        changed = true;
                        postalCodeIn.setText(charSequence.toString());
                        updatePostalCode(charSequence.toString(), position);
                    }
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    changed = false;
                }
            };
            postalCodeIn.addTextChangedListener(textWatcher);
        }

    }

    private void updateAddressLine(String charSequence, int position) {
        ((Address) getGroup(position)).setAddressLine(0, charSequence);
        notifyDataSetChanged();
    }

    private void updatePostalCode(String s, int position) {
        ((Address) getGroup(position)).setPostalCode(s);
        notifyDataSetChanged();
    }

0 个答案:

没有答案