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.




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.

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final ListView lv = getListView();


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


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;

   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(;
        tvEmail = (EditText) convertView.findViewById(;
        if (use_last_focus && currentlyFocusedRow ==position){
            if (currentlyFocusedField=="tvName")tvName.requestFocus();
            else tvEmail.requestFocus();
        tvName.setTag(position);//When focus is lost save the entered value for later use
        tvName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            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() {
            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(;
        EditText tvEmail1 = (EditText) v.findViewById(;
        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
            // ...............



Leave a Reply

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