gpt4 book ai didi

android - 在自定义 ListView 中编辑文本在滚动时失去值(value)

转载 作者:行者123 更新时间:2023-11-29 17:49:38 25 4
gpt4 key购买 nike

我知道这是重复的问题,但我没有找到正确的答案。在我的自定义 ListView 中有两个 TextView 和一个编辑文本,当我在编辑文本中插入一个值并滚动时,我丢失了编辑文本的值。当我通过列表适配器为适配器传递字符串值时,它将显示空白的编辑文本。

这是我的适配器代码:

class CreateAdapter extends ArrayAdapter<String> {
String[] strItecode=null;
String[] strItem;
String[] strQuantity,text;
Context context;
int temp;
CreateAdapter(Context context, String[] strItemcode, String[] strItem,
String[] strQauntity) {
super(context, R.layout.create_list_item, R.id.txtItemcode, strItemcode);
this.context = context;
this.strItecode = strItemcode;
this.strItem = strItem;
this.strQuantity = strQauntity;
text= new String[strItem.length];
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
temp=position;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {

holder = new ViewHolder();
convertView = mInflater
.inflate(R.layout.create_list_item, parent, false);

holder.txtItecode = (TextView) convertView
.findViewById(R.id.txtItemcode);
holder.txtItem = (TextView) convertView
.findViewById(R.id.txtItem);
holder.editQuantity = (EditText) convertView
.findViewById(R.id.editcreateQuantity);
holder.editQuantity.setTag(position);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.editQuantity.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
text[temp] = s.toString();
}

@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {

}

@Override
public void afterTextChanged(Editable s) {

}
});
holder.txtItecode.setText(strItecode[position]);
holder.txtItem.setText(strItem[position]);
holder.editQuantity.setText(text[temp]);

return convertView;
}

class ViewHolder {
TextView txtItecode;
TextView txtItem;
EditText editQuantity;
}
}

我过去 3 天都在处理这个问题,我该如何解决这个问题?

最佳答案

除了一些关于可能使用 ScrollView 的评论之外,您的代码的真正问题是 ListView 的适配器如何将 View 与您的 TextWatcher(s) 结合使用。

每次需要显示一行时,都会调用该位置上的 getView() 方法。适配器可以并且将会重用您在之前的遍历中膨胀的实际 View 以节省内存。

这通常是一件好事,但在您使用 EditTexts 和 TextWatchers 时会产生影响。

首先,holder.editQuantity.addTextChangeListener() 将继续在每个布局过程中添加一个新的 TextWatcher。这很糟糕,对您的问题负有部分责任。

其次,一旦 View 被重用,您实际上是在设置 TextWatcher 后更改其上的文本,这将同时触发旧的(如果有的话)和新的 TextWatchers 的 onTextChanged() 方法并更改您的存储的值,这反过来又会弄乱您的 View 。

如果你想使用 ListView/Adapter 路由,你的解决方案是使用一个全局 TextWatcher 变量,在 getView() 中设置文本之前从 View 中删除 TextWatcher(这样它就不会触发它的事件,当你不想要它时),然后在你设置文本后重新添加它。但是,由于您不能继续创建新的 TextWatcher(因为这会阻止您删除旧的,并且会遇到相同的值困惑问题),您还需要一种方法来链接到您的适当位置text[],因为您将无法从 getView() 方法指向它。但是,您可以设置 EditText 的 FocusChangeListener 来确定 View 何时具有焦点(即它正在被编辑)并从中设置适当的编辑位置)

例如

private int editingPosition = 0;
private TextWatcher watcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
text[editingPosition] = s.toString();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void afterTextChanged(Editable s) { }
};

public View getView(int position, View convertView, ViewGroup parent) {

// Before this is fine

holder.editQuantity.removeTextChangedListener(watcher);

holder.editQuantity.setText(text[temp]);

holder.editQuantity.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) editingPosition = position;
}
});

holder.editQuantity.addTextChangedListener(watcher);

// The rest is good, just make sure to remove the call to setting the EditText's text at the end

return convertView;
}

关于android - 在自定义 ListView 中编辑文本在滚动时失去值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23941026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com