List view edittext data loses on scrolling in Andr

2019-06-01 04:04发布

问题:

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;
}

}

回答1:

Add the listeners to the ViewHolder

 private class ViewHolder {
        TextView textView;
        EditText editQty;
        EditText editprice;
        TextWatcher qtyWatcher;
        TextWatcher priceWatcher;
        int ref;
    }

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.

@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;
        if (holder.qtyWatcher != null) {
            holder.editQty.removeTextChangedListener(holder.qtyWatcher);
        }
        if (holder.priceWatcher != null) {
            holder.editprice.removeTextChangedListener(holder.priceWatcher);
        }
        holder.textView.setText(names.get(position));
        holder.editQty.setHint("Quantity");
        holder.editprice.setHint("Price");
        if(position<quantities.size())
            holder.editQty.setText(quantities.get(position));
        else
            holder.editQty.setText("");
        if(position<itemPrices.size())
            holder.editprice.setText(itemPrices.get(position));
        else
          holder.editprice.setText("");
        holder.qtyWatcher = 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) {
                //Update the quantity
            }
        };
        holder.editQty.addTextChangedListener(holder.qtyWatcher);
        holder.priceWatcher = 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) {
                //Update you price
            }
        };
        holder.editprice.addTextChangedListener(holder.priceWatcher);

        return convertView;
    } catch (NumberFormatException ex) {
        Toast.makeText(context, "!!!", Toast.LENGTH_SHORT).show();
        ex.printStackTrace();
    }
    return convertView;
}


回答2:

You require to store update values into you data array, because list use the recycle view.

    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) {
          name.setValue(s);

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });