5

I'm struggling with a listview filled with EditText for quiet some time, and I can't find a way to achieve my task without doing something tricky.

I used a minimized project to make it simpler and easy to discuss about.

I have a ListView which is used to ask informations. The layout parts are very basic :

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.beuvelet.myapplication.MainActivity">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="beforeDescendants"/>

</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:gravity="center_vertical|center">

    <TextView
        android:id="@+id/textQuestion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|left"
        android:layout_marginLeft="10dp"
        android:text="texte"
        android:textSize="23sp"/>

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|right"
        android:gravity="center_vertical|right">

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:minWidth="125dp"/>

    </LinearLayout>
</LinearLayout>

Here's the getView of my adapter :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    v = getLayoutInflater().inflate(R.layout.list_item, null);

    EditText edit = (EditText) v.findViewById(R.id.editText);
    edit.setText(listString.get(position));

    edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
         @Override
         public void onFocusChange(View v, boolean hasFocus) {
              if (hasFocus) {
                  System.out.println("hasFocus");
              } else {
                  System.out.println("noFocus");
              }
         }
    });
    return v;
}

I want to validate the value of editText when I lose focus of it.

The problem is that each time I clic on an editText, I got this logcat :

I/System.out: hasFocus
I/System.out: noFocus
I/System.out: hasFocus
I/System.out: noFocus
I/System.out: hasFocus
I/System.out: noFocus
I/System.out: hasFocus

It goes through the OnFocusChangeListener 7 times, everytime. If there was some validation methods in the listener for the noFocus event, it could be called 3 times in a row.

Why is there so many calls ?

I could use variables, save some states, but it would be a little tricky in my situation and I prefer trying to understand this behaviour.

Thanks for reading. (sry for bad english :p)

  • In my case on Lollipop it runs over all EditTexts inside a ListView at every key press. So it calls focus change many times. – CoolMind Dec 14 '16 at 15:24
  • Not a best solution is here: http://stackoverflow.com/questions/27100534/custom-listadapter-consisting-of-edittext-lose-focus-called-twice. – CoolMind Dec 14 '16 at 15:25

1 Answers1

0

I had the exact same behaviour when navigating away from the fragment that contained my editTexts. Following this SO answer I was able to prevent onFocusChanged being called multiple times.

I just added this to my layout root view and I no longer had the issue.

<LinearLayout
    ...
    android:focusable="true"
    android:focusableInTouchMode="true"
    ...
>

I'm not sure why this worked but I'm glad it did. If anyone knows why this worked feel free to comment on this answer.

Daniel
  • 611
  • 8
  • 20