简体   繁体   中英

EditText values getting changed automatically in listView in android

I have a ListView and each row contains one EditText. When I try to change the value on row in EditText then some other rows editText value gets changed automatically.

Below is the code of listView and EditText.

       <ListView
            android:id="@+id/ListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:cacheColorHint="#ffffff"
            android:layout_above="@+id/includeID"
            android:layout_below="@+id/linearlayoutforSubTotal"
            android:visibility="invisible" >
        </ListView>

        <EditText
            android:id="@+id/editTex"
            android:layout_width="60dip"
            android:layout_height="30dip"
            android:textColor="#000000"
            android:textSize="12dip"
            android:inputType="number"
            android:editable="true" >
        </EditText>

And bellow is the code where I am trying to get the changed values of the EditText.

item.quantity.setId(position);

item.quantity.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus){

        final int position = v.getId();

        final EditText Caption = (EditText) v;

        ShoppingCartHelper.cartItems.get(position).qty =  Integer.parseInt(Caption.getText().toString());

         }                          
    }
});

Please help me find the solution for this problem

Try something like this :

editText.addTextChangedListener(new TextWatcher() {
  public void afterTextChanged(Editable s) {
    // Set the value of the field to s.toString();
  }

  public void onTextChanged(CharSequence s, int start, int before, int count) {
    // Do nothing.
  }

  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // Do nothing.
  }
});

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