Android edittext in listview loses focus on calling notifydatachanged-ThrowExceptions

Exception or error:

I have a few edittext within a listview. i have a generic focuslistener on the edittext that updates the value of the data model and also the background of the edittext when focus is lost by calling notifydatachanged . The problem is that if one of the edittext is holding focus, when i touch the next edittext, it gains focus momentarily then loses focus. I suspect it is due to the notifydatachanged method call that is causing all views to be redrawn, after which the focus is lost. Does anyone have a suggestion or work around on the issue? Thanks.

How to solve:

It is indeed happening because all the views are redrawn, so the edit text representing whatever row used to be focused is now a completely different object. Set a variable in your adapter: int currentlyFocusedRow;

in getView for your adapter: Add an onFocusChanged listener to each edit text and when that edit text gains focus, set currentlyFocusedRow = whatever row the focused edit text happens to be in. Also set any edit text that is in the currentlyFocusedRow to be focused.

###

Set

android:windowSoftInputMode="adjustPan"

for your activity in the AndroidManifest.xml

###

I also had a problem of EditText loosing the focus, but calling setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS) solved it.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final ListView lv = getListView();
    lv.setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS);
}

###

For future users, everything here is overcomplicated. The correct answer is to add

android:descendantFocusability="afterDescendants"

to your listview.

###

Example of implementing Sam Judd’s suggestion with an additional switch use_last_focus

public class my_friends_adapter extends ArrayAdapter<group_user> {
    boolean use_last_focus=false;
    int currentlyFocusedRow=-1;
    String currentlyFocusedField="";

    public my_friends_adapter(Context context, int resource, ArrayList<group_user> users) {
            super(context, resource, users);
              }
        EditText tvName,tvEmail;

    @Override
   public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_friends_item, parent, false);
        tvName = (EditText) convertView.findViewById(R.id.tvName);
        tvEmail = (EditText) convertView.findViewById(R.id.tvEmail);
        if (use_last_focus && currentlyFocusedRow ==position){
            if (currentlyFocusedField=="tvName")tvName.requestFocus();
            else tvEmail.requestFocus();
            use_last_focus=false;
        }
        tvName.setTag(position);//When focus is lost save the entered value for later use
        tvName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {save_data2( v,  hasFocus);
            }
        });

        tvEmail.setTag(position);//When focus is lost save the entered value for later use
        tvEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {save_data2( v,  hasFocus);}
        });

        return convertView;
        }


    public  void save_data2(View v, boolean hasFocus){
        int position = (Integer) v.getTag();
        EditText tvName1 = (EditText) v.findViewById(R.id.tvName);
        EditText tvEmail1 = (EditText) v.findViewById(R.id.tvEmail);
        boolean data_changed=false;
        if (hasFocus) currentlyFocusedRow=position;
        if (!(tvName1==null)) {
            if (hasFocus) currentlyFocusedField="tvName";
        }
        if (!(tvEmail1==null)) {
            if (hasFocus) currentlyFocusedField="tvEmail";
        }
        // set when data_changed
        // ..........
        if (!hasFocus && data_changed) {
            //your code to save the changed data
            // ...............
            //
            my_friends.adapter.notifyDataSetChanged();
            use_last_focus=true;
        }

    }



}

Leave a Reply

Your email address will not be published. Required fields are marked *