问题
I am using Custom ListView in My Android App. The Problem is when i am scrolling ListView, it automatically clears entered data from EditText( Edittext as a list_item ) and it ListView refreshed. I Want to Prevent it to stop clearing entered data on ListView Scroll. Please Help me..
回答1:
1) define custom list adapter
2) define a an object for each item like class MyClass { String myText}
3) store the value of the EditText of each item in that object
I have a code for storing the state of CheckBox. use and customize it for EditText.
protected interface MyTextWatcher extends TextWatcher {
public void setEnabled(EditText editText, boolean enable);
public void setText(EditText editText, String text);
}
private MyTextWatcher generalTextWatcher = new MyTextWatcher() {
private boolean enabled_edtQty = true;
private boolean enabled_edtPackingQty = true;
public void setEnabled(EditText editText, boolean enable) {
if (editText.equals(edtQty))
{
this.enabled_edtQty = enable;
}
else if (editText.equals(edtPackingQty))
{
this.enabled_edtPackingQty = enable;
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (this.enabled_edtQty && edtQty != null
&& edtQty.getText().hashCode() == s.hashCode())
{
edtQty_onTextChanged(s, start, before, count);
}
else if (this.enabled_edtPackingQty && edtPackingQty != null
&& edtPackingQty.getText().hashCode() == s.hashCode())
{
edtPackingQty_onTextChanged(s, start, before, count);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void setText(EditText editText, String text) {
this.setEnabled(editText, false);
editText.setText(text);
this.setEnabled(editText, true);
}
};
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
final MyClass item = getItem(position);
edtQty.addTextChangedListener(generalTextWatcher);
edtPackingQty.addTextChangedListener(generalTextWatcher);
generalTextWatcher.setText(edtQty, item.myText1);
generalTextWatcher.setText(edtPackingQty, item.myText2);
}
protected void edtQty_onTextChanged(CharSequence s, int start, int before,
int count) {
item.myText1 = s;
}
protected void edtPackingQty_onTextChanged(CharSequence s, int start, int before,
int count) {
item.myText2 = s;
}
in your situation you should use addTextChangedListener
instead of setOnCheckedChangeListener
回答2:
the answer by breceivemail is correct,
you can also try using ArrayList for storing data onTextChangeListener can store data when needed and you can restore data in getView of your listview's adapter
Note: if you are searching for stopping the listview from refreshing on scrolling, then you are going in wrong direction, because we can't stop listview from refreshing its contents(because all off screen contents are dumped in listviews), adapters are made to store/provide data when needed by listview(you can't rely on system to protect/store data you should store/restore data manually).
来源:https://stackoverflow.com/questions/14169921/prevent-custom-listview-refreshing-on-scroll-i-want-to-stop-refreshing