addTextChangedListener()不在listview android中工作

时间:2016-02-22 13:36:02

标签: android listview android-edittext

public View getView(final int position, View convertView, final ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.drop_location_card, parent, false);
    }

    final ReceiverInformation currentItem = getItem(position);

    final EditText receiverName = (EditText) view.findViewById(R.id.receiver_name);
    final EditText receiverNumber = (EditText) view.findViewById(R.id.receiver_phone_number);
    final TextView receiverAddress = (TextView) view.findViewById(R.id.receiver_address);
    final CheckBox copyDetailCheckBox = (CheckBox) view.findViewById(R.id.copy_detail);
    final ImageView deleteItem = (ImageView) view.findViewById(R.id.delete);
    if(position == 0) {
        deleteItem.setVisibility(View.GONE);
    } else {
        deleteItem.setVisibility(View.VISIBLE);
    }

    receiverName.setText(currentItem.getName());
    receiverNumber.setText(currentItem.getNumber());
    receiverAddress.setText(currentItem.getAddress());

    receiverName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mLastFocussedPosition == -1 || mLastFocussedPosition == position) {
                            mLastFocussedPosition = position;
                            receiverName.requestFocus();
                        }
                    }
                }, 200);

            } else {
                mLastFocussedPosition = -1;
            }
        }
    });

    receiverName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            InputMethodManager inputManager = (InputMethodManager)
                    mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow((null == mContext.getCurrentFocus()) ?
                    null : mContext.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            if (actionId == EditorInfo.IME_ACTION_GO || actionId == EditorInfo.IME_ACTION_DONE) {
                currentItem.setName(String.valueOf(v.getText()));
                ((BookingDetailActivity) mContext).updateReceiverInformation(position);
                notifyDataSetChanged();
                return true;
            } else {
                return false;
            }
        }
    });

    receiverName.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) {
            currentItem.setName(String.valueOf(s));
            ((BookingDetailActivity) mContext).updateReceiverInformation(position);
            notifyDataSetChanged();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    receiverNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            InputMethodManager inputManager = (InputMethodManager)
                    mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow((null == mContext.getCurrentFocus()) ?
                    null : mContext.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            if (actionId == EditorInfo.IME_ACTION_GO || actionId == EditorInfo.IME_ACTION_DONE) {
                currentItem.setNumber(String.valueOf(v.getText()));
                ((BookingDetailActivity) mContext).updateReceiverInformation(position);
                notifyDataSetChanged();
                return true;
            } else {
                return false;
            }
        }
    });

    receiverNumber.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) {
            currentItem.setNumber(String.valueOf(receiverAddress.getText()));
            ((BookingDetailActivity) mContext).updateReceiverInformation(position);
            notifyDataSetChanged();
        }
    });

    receiverAddress.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mLastSelectedPosition = position;
            Intent autoComplete = new Intent(LtAceApplication.getContext(), AutoCompleteActivity.class);
            autoComplete.putExtra("TAG", "drop");
            mContext.startActivity(autoComplete);
        }
    });

    copyDetailCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (copyDetailCheckBox.isChecked()) {
                currentItem.setName(mCustomerName);
                currentItem.setNumber(mCustomerNumber);
                ((BookingDetailActivity) mContext).updateReceiverInformation(position);
            } else {
                currentItem.setName("");
                currentItem.setNumber("");
            }
            notifyDataSetChanged();
        }
    });

    deleteItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mDropLocations.remove(position);
            ((BookingDetailActivity)mContext).onItemDeleted(position);
            notifyDataSetChanged();
        }
    });

    return view;
}

我有一个listview,列表视图的每个项目都有两个edittext ..我正在尝试在edittext上实现addTextChangedListener,但它不起作用,我认为它与一些焦点问题有关,listview需要当我点击一个edittext时焦点..任何人都有同样的问题??

1 个答案:

答案 0 :(得分:0)

尝试添加焦点检查,如下所示:

if (receiverName.hasFocus()) {
    currentItem.setName(String.valueOf(s));
    ((BookingDetailActivity)).updateReceiverInformation(position);
    notifyDataSetChanged();
}