So, I have a custom adapter with two EditText fields on each line.
I got most of the material working properly, except for storing values ββinside an ArrayList.
This is the code I've done so far:
private void holderTitleSavedOnScroll(final int position, IZUICartViewHolder holder) {
if (!(position == (variantArrayList.size() - 1)) && holder.title != null) {
holder.title.setText(variantArrayList.get(position).getVariantTitle());
final int finalPosition = position;
holder.title.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
final EditText newVariant = (EditText) v;
variantArrayList.get(finalPosition).setVariantTitle(newVariant.getText().toString());
}
});
}
}
So it really does what I want, it retains value when the focus has changed. With the exception of one problem, it retains value only when the focus changes.
This is great in most cases, unless the user actually clicks the button, which causes the entire view to disappear. The focus has not changed and the value will not be set.
So, I assume that you all think, let's just call addOnTextChangedListener and attach a TextWatcher by adding something like this:
holder.title.setText(variantArrayList.get(position).getVariantTitle());
final int finalPosition = position;
final EditText holderTitle = (EditText) holder.title;
if (holderTitle.getTag() != null) {
final TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
variantArrayList.get(finalPosition).setVariantTitle(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
};
holder.title.addTextChangedListener(textWatcher);
holder.title.setTag(true);
}
, .
, , , listview , , , ArrayList.
, , ( ).
?
( ):
TextWatcher, :
getView ( ):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
IZUICartViewHolder holder;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (v == null) {
holder = new IZUICartViewHolder();
int type = getItemViewType(position);
switch (type) {
case TYPE_EDIT:
v = inflater.inflate(R.layout.iz_ui_modify_product_cell, parent, false);
holder.title = (EditText) v.findViewById(R.id.iz_prod_modify_variant_title);
holder.title.setHint(addVariantPlaceholder);
holder.deleteButton = v.findViewById(R.id.click_remove);
holder.price = (EditText) v.findViewById(R.id.iz_prod_modify_price);
holder.price.setHint(pricePlaceholder);
holder.price.setText(String.valueOf(0.0));
break;
}
v.setTag(holder);
} else {
holder = (IZUICartViewHolder) v.getTag();
}
hideDeleteButton(holder, position);
holderTitleSavedOnScroll(position, holder);
holderPriceSavedOnScroll(position, holder);
v.setTag(holder);
return v;
}
holderTitleSavedOnScroll
private void holderTitleSavedOnScroll(final int position, IZUICartViewHolder holder) {
if (!(position == (variantArrayList.size() - 1)) && holder.title != null) {
holder.title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.requestFocus();
}
});
final int finalPosition = position;
final EditText holderTitle = (EditText) holder.title;
if (holderTitle != null) {
holder.title.setText(variantArrayList.get(position).getVariantTitle());
}
holderTitle.addTextChangedListener(new EditVariantTextWatcher(variantArrayList.get(finalPosition)));
}
}
TextWatcher:
public class EditVariantTextWatcher implements TextWatcher {
private IZUIProductVariantContainer variantContainer;
protected EditVariantTextWatcher(IZUIProductVariantContainer variantContainer) {
this.variantContainer = variantContainer;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
variantContainer.setVariantTitle(s.toString());
}
}