当我如此快速地向下/向上滚动时,位置变化得太快以至于弄乱了该位置的项目,导致文本改变而不是它应该是什么。我一直在寻找修复,但似乎无法确定它出错的地方。下面是我的Custom ArrayAdapter代码
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import aungkyawpaing.ngapyinkeyboard.Models.CustomKey;
import aungkyawpaing.ngapyinkeyboard.R;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by Vincent on 16-Apr-15.
*/
public class CustomKeyAdapter extends ArrayAdapter<CustomKey> {
private int size;
ArrayList<CustomKey> datas = new ArrayList<>();
public CustomKeyAdapter(Context context, ArrayList<CustomKey> list) {
super(context, R.layout.listitem_customkey, list);
size = list.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
LayoutInflater layoutInflater =
(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listitem_customkey, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
CustomKey key = getItem(position);
datas.add(position, key);
viewHolder.txt_number.setText("Key #" + (position + 1) + " : ");
viewHolder.value_field.setText(key.getOutput());
viewHolder.value_field.addTextChangedListener(new MyTextWatcher(position));
return convertView;
}
private class MyTextWatcher implements TextWatcher {
private int position;
public MyTextWatcher(int position) {
this.position = position;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
CustomKey key = getItem(position);
key.setOutput(editable.toString());
datas.add(position, key);
}
}
static class ViewHolder {
@InjectView(R.id.custom_key_number) TextView txt_number;
@InjectView(R.id.custom_key_value) EditText value_field;
public ViewHolder(View view) {
ButterKnife.inject(this, view);
}
}
public ArrayList<CustomKey> getValues() {
return datas;
}
}
编辑:TextView很好,只是EditText中的文本不断变化
答案 0 :(得分:1)
在将TextWatcher
添加到EditText
之前,请移除TextWatcher
的现有EditText
。
在TextWatcher
static class ViewHolder {
@InjectView(R.id.custom_key_number) TextView txt_number;
@InjectView(R.id.custom_key_value) EditText value_field;
MyTextWatcher myWatcher;
public ViewHolder(View view) {
ButterKnife.inject(this, view);
}
}
现在删除TextWatcher
的现有EditText
,在TextWatcher
方法中初始化视图持有者的getView()
,并将TextWatcher
添加到EditText
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
LayoutInflater layoutInflater =
(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listitem_customkey, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
CustomKey key = getItem(position);
datas.add(position, key);
viewHolder.txt_number.setText("Key #" + (position + 1) + " : ");
viewHolder.value_field.setText(key.getOutput());
//Remove the myWatcher
viewHolder.value_field.emoveTextChangedListener(myWatcher);
viewHolder.myWatcher=new MyTextWatcher(position);
//add the myWatcher
viewHolder.value_field.addTextChangedListener(myWatcher);
return convertView;
}
答案 1 :(得分:0)
我已经解决了这个问题。我似乎在&#34; onTextChanged&#34;中,键是引用视图中的项目。因此setOutput会更改键输出。修复了以下代码
的问题public void afterTextChanged(Editable editable) {
CustomKey key = getItem(position);
datas.add(position, new CustomKey(key.getCode(), editable.toString()));
}