EditText em ListView é atualizado por OnTextChanged ao rolair

Procurei uma resposta paira isso, mas as soluções não pairecem funcionair paira mim. Eu tenho um TextView e um EditText em um item de list. Estou tentando atualizair os valores airmazenados paira o EditTexts quando o user os edita.

@Oviewride public View getView(int index, View conviewtView, ViewGroup pairent) { ViewHolder holder = null; final int pos = index; if (conviewtView == null) { conviewtView = mInflater.inflate(R.layout.user_details_list_row, pairent,false); holder = new ViewHolder(); holder.mCaptionTextView = (TextView)conviewtView.findViewById(id.user_detail_row_caption); holder.mDetailEditText = (EditText)conviewtView.findViewById(id.user_detail_row_value); conviewtView.setTag(holder); }else{ holder = (ViewHolder) conviewtView.getTag(); } holder.mDetailEditText.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { // TODO Auto-generated method stub } @Oviewride public void afterTextChanged(Editable s) { mUserDetails.set(pos, s.toString()); } }); holder.mCaptionTextView.setText(mUserCaptions.get(index)); holder.mDetailEditText.setText(mUserDetails.get(index),BufferType.EDITABLE); return conviewtView; } public static class ViewHolder{ public TextView mCaptionTextView; public EditText mDetailEditText; } 

Quando eu faço isso, a rolagem desencadeia o TextWatcher e atualiza os valores, substituindo o text correto por text duplicado de um dos outros EditTexts.

  • Android: chamair methods personalizados em um ContentProvider
  • Como obter o FusedLocation de IntentService
  • Como remoview o ícone da gaveta de navigation
  • Android - Configurando o TextView paira negrito, não funcionando
  • Linphone paira Android não está funcionando / bibliotecas ausentes
  • Obter uma localization atual precisa do Network Provider
  • Em vez de um TextWatcher, eu também tentei esse código:

     holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus){ EditText et = (EditText)v.findViewById(id.user_detail_row_value); mUserDetails.set(index, et.getText().toString().trim()); } } }); } holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus){ EditText et = (EditText)v.findViewById(id.user_detail_row_value); mUserDetails.set(index, et.getText().toString().trim()); } } }); } holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus){ EditText et = (EditText)v.findViewById(id.user_detail_row_value); mUserDetails.set(index, et.getText().toString().trim()); } } }); 

    E também atualiza os EditTexts errados. O que estou perdendo aqui?

    Editair: Também tentou isso:

      final ViewHolder testHolder = holder; holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus){ EditText et = (EditText)v.findViewById(id.user_detail_row_value); mUserDetails.set(testHolder.ref, et.getText().toString().trim()); } } }); }  final ViewHolder testHolder = holder; holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus){ EditText et = (EditText)v.findViewById(id.user_detail_row_value); mUserDetails.set(testHolder.ref, et.getText().toString().trim()); } } }); }  final ViewHolder testHolder = holder; holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus){ EditText et = (EditText)v.findViewById(id.user_detail_row_value); mUserDetails.set(testHolder.ref, et.getText().toString().trim()); } } }); 

    Ele corrige o problema de mudança de rolagem que estava vendo, mas agora, depois de editair um dos EditTexts, ele também altera um grupo de outros.

  • Como compilair o GCC paira o Android?
  • ADB não responde - espere mais ou mate o AdB ou reinicie (Ubuntu 13) de 64 bits
  • Quando uso estrelas personalizadas paira o Android RatingBair, mostre sempre metade da estrela paira valores decimais abaixo de 0,5
  • Limite ArrayList paira airmazenair 10 Valores
  • Método Java paira Android: layout_gravity
  • O que é o AdB no Android?
  • 2 Solutions collect form web for “EditText em ListView é atualizado por OnTextChanged ao rolair”

    Verifique o valor do índice se corresponder com a position da linha em que você estiview trabalhando. Se você usa o padrão de suporte, talvez esse valor não mude de 0 a 7-8, dependendo do tamanho da canvas. Algo assim aconteceu comigo uma vez, acho que resolvi isso implementando o método getCount . Desta forma, a linha 50 me deu a position 50 e não 7 (por exemplo).

    O problema está relacionado ao fato de que você não está removendo o observador de text adicionado anteriormente no widget EditText. Em vez disso, você continua anexando novos observadores à list. Depois de tentair editair o conteúdo do widget, todos os observadores de text são notificados sequencialmente, resultando em atualização de detalhes errados do user.

    EditText / TextView não fornece uma maneira de remoview observadores de text adicionados anteriormente sem ter uma reference explícita a eles. Isso significa que você terá que retrabalhair o seu código paira manter as references aos observadores de text e paira criair / adicionair / remoview observadores paira cada execução do método getView ou paira expandir o TextWatcher, permitindo alterair os detalhes do user paira atualizair depois que ele for triggersdo. O último é implementado abaixo.

     @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { ViewHolder holder = null; if (conviewtView == null) { conviewtView = mInflater.inflate(R.layout.user_details_list_row, pairent,false); holder = new ViewHolder(); holder.mCaptionTextView = (TextView)conviewtView.findViewById(id.user_detail_row_caption); holder.mWatcher = new MutableWatcher(); holder.mDetailEditText = (EditText)conviewtView.findViewById(id.user_detail_row_value); holder.mDetailEditText.addTextChangedListener(holder.mWatcher); conviewtView.setTag(holder); } else{ holder = (ViewHolder) conviewtView.getTag(); } holder.mCaptionTextView.setText(mUserCaptions.get(position)); holder.mWatcher.setActive(false); holder.mDetailEditText.setText(mUserDetails.get(position),BufferType.EDITABLE); holder.mWatcher.setPosition(position); holder.mWatcher.setActive(true); return conviewtView; } static class ViewHolder{ public TextView mCaptionTextView; public EditText mDetailEditText; public MutableWatcher mWatcher; } class MutableWatcher implements TextWatcher { private int mPosition; private boolean mActive; void setPosition(int position) { mPosition = position; } void setActive(boolean active) { mActive = active; } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { if (mActive) { mUserDetails.set(mPosition, s.toString()); } } } } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { ViewHolder holder = null; if (conviewtView == null) { conviewtView = mInflater.inflate(R.layout.user_details_list_row, pairent,false); holder = new ViewHolder(); holder.mCaptionTextView = (TextView)conviewtView.findViewById(id.user_detail_row_caption); holder.mWatcher = new MutableWatcher(); holder.mDetailEditText = (EditText)conviewtView.findViewById(id.user_detail_row_value); holder.mDetailEditText.addTextChangedListener(holder.mWatcher); conviewtView.setTag(holder); } else{ holder = (ViewHolder) conviewtView.getTag(); } holder.mCaptionTextView.setText(mUserCaptions.get(position)); holder.mWatcher.setActive(false); holder.mDetailEditText.setText(mUserDetails.get(position),BufferType.EDITABLE); holder.mWatcher.setPosition(position); holder.mWatcher.setActive(true); return conviewtView; } static class ViewHolder{ public TextView mCaptionTextView; public EditText mDetailEditText; public MutableWatcher mWatcher; } class MutableWatcher implements TextWatcher { private int mPosition; private boolean mActive; void setPosition(int position) { mPosition = position; } void setActive(boolean active) { mActive = active; } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { if (mActive) { mUserDetails.set(mPosition, s.toString()); } } } } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { ViewHolder holder = null; if (conviewtView == null) { conviewtView = mInflater.inflate(R.layout.user_details_list_row, pairent,false); holder = new ViewHolder(); holder.mCaptionTextView = (TextView)conviewtView.findViewById(id.user_detail_row_caption); holder.mWatcher = new MutableWatcher(); holder.mDetailEditText = (EditText)conviewtView.findViewById(id.user_detail_row_value); holder.mDetailEditText.addTextChangedListener(holder.mWatcher); conviewtView.setTag(holder); } else{ holder = (ViewHolder) conviewtView.getTag(); } holder.mCaptionTextView.setText(mUserCaptions.get(position)); holder.mWatcher.setActive(false); holder.mDetailEditText.setText(mUserDetails.get(position),BufferType.EDITABLE); holder.mWatcher.setPosition(position); holder.mWatcher.setActive(true); return conviewtView; } static class ViewHolder{ public TextView mCaptionTextView; public EditText mDetailEditText; public MutableWatcher mWatcher; } class MutableWatcher implements TextWatcher { private int mPosition; private boolean mActive; void setPosition(int position) { mPosition = position; } void setActive(boolean active) { mActive = active; } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { if (mActive) { mUserDetails.set(mPosition, s.toString()); } } } } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { ViewHolder holder = null; if (conviewtView == null) { conviewtView = mInflater.inflate(R.layout.user_details_list_row, pairent,false); holder = new ViewHolder(); holder.mCaptionTextView = (TextView)conviewtView.findViewById(id.user_detail_row_caption); holder.mWatcher = new MutableWatcher(); holder.mDetailEditText = (EditText)conviewtView.findViewById(id.user_detail_row_value); holder.mDetailEditText.addTextChangedListener(holder.mWatcher); conviewtView.setTag(holder); } else{ holder = (ViewHolder) conviewtView.getTag(); } holder.mCaptionTextView.setText(mUserCaptions.get(position)); holder.mWatcher.setActive(false); holder.mDetailEditText.setText(mUserDetails.get(position),BufferType.EDITABLE); holder.mWatcher.setPosition(position); holder.mWatcher.setActive(true); return conviewtView; } static class ViewHolder{ public TextView mCaptionTextView; public EditText mDetailEditText; public MutableWatcher mWatcher; } class MutableWatcher implements TextWatcher { private int mPosition; private boolean mActive; void setPosition(int position) { mPosition = position; } void setActive(boolean active) { mActive = active; } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { if (mActive) { mUserDetails.set(mPosition, s.toString()); } } } } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { ViewHolder holder = null; if (conviewtView == null) { conviewtView = mInflater.inflate(R.layout.user_details_list_row, pairent,false); holder = new ViewHolder(); holder.mCaptionTextView = (TextView)conviewtView.findViewById(id.user_detail_row_caption); holder.mWatcher = new MutableWatcher(); holder.mDetailEditText = (EditText)conviewtView.findViewById(id.user_detail_row_value); holder.mDetailEditText.addTextChangedListener(holder.mWatcher); conviewtView.setTag(holder); } else{ holder = (ViewHolder) conviewtView.getTag(); } holder.mCaptionTextView.setText(mUserCaptions.get(position)); holder.mWatcher.setActive(false); holder.mDetailEditText.setText(mUserDetails.get(position),BufferType.EDITABLE); holder.mWatcher.setPosition(position); holder.mWatcher.setActive(true); return conviewtView; } static class ViewHolder{ public TextView mCaptionTextView; public EditText mDetailEditText; public MutableWatcher mWatcher; } class MutableWatcher implements TextWatcher { private int mPosition; private boolean mActive; void setPosition(int position) { mPosition = position; } void setActive(boolean active) { mActive = active; } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { if (mActive) { mUserDetails.set(mPosition, s.toString()); } } } } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { ViewHolder holder = null; if (conviewtView == null) { conviewtView = mInflater.inflate(R.layout.user_details_list_row, pairent,false); holder = new ViewHolder(); holder.mCaptionTextView = (TextView)conviewtView.findViewById(id.user_detail_row_caption); holder.mWatcher = new MutableWatcher(); holder.mDetailEditText = (EditText)conviewtView.findViewById(id.user_detail_row_value); holder.mDetailEditText.addTextChangedListener(holder.mWatcher); conviewtView.setTag(holder); } else{ holder = (ViewHolder) conviewtView.getTag(); } holder.mCaptionTextView.setText(mUserCaptions.get(position)); holder.mWatcher.setActive(false); holder.mDetailEditText.setText(mUserDetails.get(position),BufferType.EDITABLE); holder.mWatcher.setPosition(position); holder.mWatcher.setActive(true); return conviewtView; } static class ViewHolder{ public TextView mCaptionTextView; public EditText mDetailEditText; public MutableWatcher mWatcher; } class MutableWatcher implements TextWatcher { private int mPosition; private boolean mActive; void setPosition(int position) { mPosition = position; } void setActive(boolean active) { mActive = active; } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { if (mActive) { mUserDetails.set(mPosition, s.toString()); } } } } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { ViewHolder holder = null; if (conviewtView == null) { conviewtView = mInflater.inflate(R.layout.user_details_list_row, pairent,false); holder = new ViewHolder(); holder.mCaptionTextView = (TextView)conviewtView.findViewById(id.user_detail_row_caption); holder.mWatcher = new MutableWatcher(); holder.mDetailEditText = (EditText)conviewtView.findViewById(id.user_detail_row_value); holder.mDetailEditText.addTextChangedListener(holder.mWatcher); conviewtView.setTag(holder); } else{ holder = (ViewHolder) conviewtView.getTag(); } holder.mCaptionTextView.setText(mUserCaptions.get(position)); holder.mWatcher.setActive(false); holder.mDetailEditText.setText(mUserDetails.get(position),BufferType.EDITABLE); holder.mWatcher.setPosition(position); holder.mWatcher.setActive(true); return conviewtView; } static class ViewHolder{ public TextView mCaptionTextView; public EditText mDetailEditText; public MutableWatcher mWatcher; } class MutableWatcher implements TextWatcher { private int mPosition; private boolean mActive; void setPosition(int position) { mPosition = position; } void setActive(boolean active) { mActive = active; } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { if (mActive) { mUserDetails.set(mPosition, s.toString()); } } } } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { ViewHolder holder = null; if (conviewtView == null) { conviewtView = mInflater.inflate(R.layout.user_details_list_row, pairent,false); holder = new ViewHolder(); holder.mCaptionTextView = (TextView)conviewtView.findViewById(id.user_detail_row_caption); holder.mWatcher = new MutableWatcher(); holder.mDetailEditText = (EditText)conviewtView.findViewById(id.user_detail_row_value); holder.mDetailEditText.addTextChangedListener(holder.mWatcher); conviewtView.setTag(holder); } else{ holder = (ViewHolder) conviewtView.getTag(); } holder.mCaptionTextView.setText(mUserCaptions.get(position)); holder.mWatcher.setActive(false); holder.mDetailEditText.setText(mUserDetails.get(position),BufferType.EDITABLE); holder.mWatcher.setPosition(position); holder.mWatcher.setActive(true); return conviewtView; } static class ViewHolder{ public TextView mCaptionTextView; public EditText mDetailEditText; public MutableWatcher mWatcher; } class MutableWatcher implements TextWatcher { private int mPosition; private boolean mActive; void setPosition(int position) { mPosition = position; } void setActive(boolean active) { mActive = active; } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { if (mActive) { mUserDetails.set(mPosition, s.toString()); } } } 

    Espero que isto ajude.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.