I want the listview having 2 edittexts to preserve the values even after becoming invisible after scrolling. And when the edittext losses focus, I want each edittext value to be saved in a arraylist (2 arraylist - one for quantites and one for prices), which I can later save to the database. I tried having the code in the OntextChanegd method, but it doesn't seem right.
public class CustomAdapter extends BaseAdapter {
ArrayList<String> names;
Context context;
ArrayList<String> itemPrices = new ArrayList<>();
ArrayList<String> quantities = new ArrayList<>();
CustomAdapter(ArrayList v, Context c) {
names = v;
context = c;
}
@Override
public int getCount() {
return names.size();
}
@Override
public Object getItem(int position) {
return names.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
try {
final ViewHolder holder;
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
holder.textView = (TextView) convertView.findViewById(R.id.rowText);
holder.editQty = (EditText) convertView.findViewById(R.id.qty);
holder.editprice = (EditText) convertView.findViewById(R.id.price);
holder.textView.setTextSize(20);
holder.textView.setTextColor(Color.BLACK);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView.setText(names.get(position));
holder.editQty.setHint("Quantity");
holder.editprice.setHint("Price");
holder.editQty.addTextChangedListener(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) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
holder.editprice.addTextChangedListener(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) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
return convertView;
}catch (NumberFormatException ex){
Toast.makeText(context,"!!!",Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
return convertView;
}
private class ViewHolder {
TextView textView;
EditText editQty;
EditText editprice;
int ref;
}
}
You require to store update values into you data array, because list use the recycle view.
Add the listeners to the
ViewHolder
Now in the getView() method remove the existing watches of the edittext , then update the text in the edittext and set the new text watchers.