5

I am making a E-commerce app whose cart list has a custom ListView which consist of EditText. The EditText represents the quantity of item. I am using OnFocusChangeListener to detect when a customer is done changing the quantity of the item then updating the cart on server. Everything is working fine, just the onFocusChange is being called twice i.e. I am getting false twice.

viewHolder.etProductQuantity.setOnFocusChangeListener( new View.OnFocusChangeListener() {

    @Override
    public void onFocusChange(View view, boolean hasFocus) {

        if(!hasFocus){

            // Updating the ProductList class's object to set the new quantity
            // Updating product quantity on server
            Log.d("Product Quantity", viewHolder.etProductQuantity.getText().toString() + b);
        }
    }
});

Thus, the coding is being executed twice which is creating problem.

Luciano Rodríguez
  • 2,239
  • 3
  • 19
  • 32
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107

3 Answers3

2

Adding the following line into your activity in the manifest fixed the problem:

 android:windowSoftInputMode="adjustPan"

Not sure why though.

Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
1

You may use like this:

edt_sys_log_search.setOnEditorActionListener(new OnEditorActionListener() {        
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId==EditorInfo.IME_ACTION_DONE){
                    //do calling WS                 
                }
                return false;
            }
        });

and set EditText property as follows:

android:imeOptions="actionDone"
singleLine="true"

It will call WS when user press Done from SoftKeyboard

Disposer
  • 6,201
  • 4
  • 31
  • 38
  • For some reasons, onEditorAction is not being triggered. I am using genymotion right now and it doesn't have a soft keyboard so that might be the issue, I'll check on real device and let you know. – Rohan Kandwal Nov 24 '14 at 14:51
1

Try to maintain one flag which check is lost focus code executed one time then never executed again :

viewHolder.etProductQuantity.setTag(1);
viewHolder.etProductQuantity.setOnFocusChangeListener( new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if(!hasFocus && ((Integer)view.getTag())==1){
                    // Updating the ProductList class's object to set the new quantity
                    // Updating product quantity on server
                    Log.d("Product Quantity",                    
                    viewHolder.etProductQuantity.getText().toString() + b);
                    view.setTag(0);
                }else{
                    view.setTag(1);
                }
            }
        });
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67