这个问题经常被问到..但似乎没有什么对我有用。
我有一个带有edittext的listview ..当我滚动edittext重复的listview值时。
请建议!!
代码:
public View getView(int position, View view, ViewGroup parent) {
View mView = view; //trying to reuse a recycled view
if (mView == null) {
//The view is not a recycled one: we have to inflate
mView = getActivity().getLayoutInflater().inflate(
getResource(), parent, false);
holder = new ViewHolder();
holder.row = (OEDataRow) moveLinesfinalData.get(position);
holder.eProductName = (TextView) mView.findViewById(R.id.textViewProductNameFinal);
holder.eProductName.setText(holder.row.getString("name").toString());
holder.innerLinearLayout = (LinearLayout) mView
.findViewById(R.id.innerLinearLayout);
holder.editTextProductId = (EditText) holder.innerLinearLayout
.findViewById(R.id.editTextProductId);
holder.eProductSerial = (EditText) holder.innerLinearLayout
.findViewById(R.id.editTextSerialFinal);
holder.BarCode = (Button) holder.innerLinearLayout.findViewById(R.id.buttonBarcode);
holder.editTextProductIdFocus = (EditText) holder.innerLinearLayout
.findViewById(R.id.editTextProductId);
mView.setTag(holder);
} else {
// View recycled !
// no need to inflate
// no need to findViews by id
holder = (ViewHolder) mView.getTag();
}
if(holder.row != null) {
holder.editTextProductId.setText(holder.row.getInt("id").toString());
}
holder.eProductSerial.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
String newSerial = s.toString();
String id = holder.editTextProductIdFocus.getText().toString();
productData.put(id,newSerial);
}
});
Log.d("list", "list productData "+productData);
holder.BarCode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.getId();
}
});
return mView;
}
答案 0 :(得分:0)
您必须保存一个字符串数组(类似arrayStrings
),其中包含当前位置的EditText值。
在textChangedListener上设置arrayStrings[position]
的内容。接下来在getView上将editText的queryText设置为数组中当前位置的任何内容。否则,由于视图将被回收,因此会出现不正确的行为。
答案 1 :(得分:0)
而不是
if(holder.row != null) {
holder.editTextProductId.setText(holder.row.getInt("id").toString());
}
成功
if(holder.row != null) {
holder.editTextProductId.setText(((OEDataRow) moveLinesfinalData.get(position)).getInt("id"));
}
在HolderView中只保留视图ID,而不保留一些数据本身。
答案 2 :(得分:0)
删除:
holder.row = (OEDataRow) moveLinesfinalData.get(position);
并更改
if(holder.row != null) {
holder.editTextProductId.setText(holder.row.getInt("id").toString());
}
到
if((OEDataRow) moveLinesfinalData.get(position) != null) {
holder.editTextProductId.setText(String.valueOf((OEDataRow) moveLinesfinalData.get(position)).getInt("id")));
}