简体   繁体   中英

Currency format only working for one dynamic EditText and not for the others

The following for loop adds x amount of rows depending on the size of items:

for (int i = 0; i < shoppingQuantityAndItems.size(); i++) {
    TableRow label = new TableRow(this);

    EditText quantity = new EditText(this);
    quantity.setInputType(InputType.TYPE_CLASS_NUMBER);
    quantity.setText(shoppingQuantityAndItems.get(i).get(0));
    System.out.println("Size: "
            + shoppingQuantityAndItems.get(i).get(0));
    label.addView(quantity);

    TextView items = new TextView(this);
    items.setText(shoppingQuantityAndItems.get(i).get(1));
    System.out.println("Item: "
            + shoppingQuantityAndItems.get(i).get(1));
    label.addView(items);

    price = new EditText(this);
    price.setId(i);
    price.setInputType(InputType.TYPE_CLASS_NUMBER
            | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    price.setText("");
    label.addView(price);
    updatePrice(price.getId());

    CheckBox ch = new CheckBox(this);
    ch.setChecked(false);
    label.addView(ch);

    table.addView(label);
}

Because I want each different price EditText field to have different values when the user inputs them, I call a method with their unique ID:

public void updatePrice(int id){
    updatedPrice = (EditText) price.findViewById(id);
    updatedPrice.addTextChangedListener(new TextWatcher() {

        private String current = "";

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!s.toString().equals(current)) {
                updatedPrice.removeTextChangedListener(this);
                String cleanString = s.toString().replaceAll("[£,.]", "");
                double parsed = Double.parseDouble(cleanString);           
                String formated = NumberFormat.getCurrencyInstance()
                        .format((parsed/100));          
                current = formated;
                updatedPrice.setText(formated);
                updatedPrice.setSelection(formated.length());
                updatedPrice.addTextChangedListener(this);
            }
        }

I was just wondering if anyone could tell me how to make sure when I input a number into any of these text fields, then only that value changes, and not the others.

You are using the wrong EditText widget.

updatedPrice presumably is a data member of your class. This is not a good idea, as you do not have just one of these EditText widgets.

Moreover, your onTextChanged() assumes that updatedPrice will be pointing at the right EditText widget -- the one the user happens to be typing into -- and statistically that is unlikely.

Assuming that the rest of the code is actually what you want, then you need to change:

updatedPrice = (EditText) price.findViewById(id);

to be:

final EditText updatedPrice = (EditText) price.findViewById(id);

This will cause your TextWatcher anonymous inner class to use the updatedPrice you retrieve at the time of the updatePrice() call, not some random EditText that you have in a data member of the class.

Or, you could have the TextWatcher do the findViewById() itself. Either way should work.

I would recommend just getting rid of the updatedPrice data member and fix up the rest of your code, as you may have other similar sorts of bugs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM