Android - ListView EditText Focus Issues

I have an EditText that I'm using as the footer View for my ListView. I'm attempting to access the focus / lose focus events, but for some reason, it's firing multiple times per focus. What is going on here?

// in onCreate

// ...

// the footer
View v = getLayoutInflater().inflate( R.layout.comment_edittext, null );

EditText commentEditText = (EditText)v.findViewById( R.id.comment_edittext );
commentEditText.setOnFocusChangeListener( new OnFocusChangeListener() {

    @Override
    public void onFocusChange( View v, boolean hasFocus )
    {
        U.log("View: " + v.getClass().getName().toString() );

        if( hasFocus )
        {
            U.log( "Clicked" );
        }
        else
        {
            U.log( "Un Clicked" );
        }
    }
} );

// add the footer
commentListView.addFooterView( v );

// ...

The output, of a single touch, which brings up the keyboard is:

04-11 10:22:17.449: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.459: E/004 - X(4576): Clicked
04-11 10:22:17.569: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.569: E/004 - X(4576): Un Clicked
04-11 10:22:17.569: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.569: E/004 - X(4576): Clicked
04-11 10:22:17.689: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.689: E/004 - X(4576): Un Clicked
04-11 10:22:17.709: E/004 - X(4576): View: android.widget.EditText
04-11 10:22:17.709: E/004 - X(4576): Clicked

I can kind of understand it calling multiple events with hasFocus set to true, but why then are there also falses, triggering the Un Clicked log?

EDIT: Furthermore, whenever I scroll the ListView up and down, moving the EditText in and out of view, it's calling these events as well. Am I using the wrong listener type perhaps?

Answers


From personal experience, EditTexts inside ListViews are a bane. Because of the way views are recycled and rendered, the focus behaves strangely. I recommend approaching the problem some other way. Actually, if it's a comment text box, wouldn't it be better if it was always visible?

Sample layout:

  <LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> 
    <ListView 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1"/> 
    <EditText 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"/>  
  </LinearLayout>

Use a GridView instead and give a numColumns of 1. If you don't like how that reads in your java classes, store them as an AbsListView (and just keep them as a GridView in your xml).

ListView is buggy with EditTexts, GridViews lack those problems.


Using Grideview with a numColumns of 1 is the perfect way to solove the problem. I have try it and solve my problem .


Need Your Help

Hide, show annotation on MkMapView

iphone objective-c cocoa cocoa-touch iphone-sdk-3.0

How to hide annotation when zooming out the map view. I have a big number of annotation i have to hide them because if the region displayed on the map is too big you can see only the annotations.

Establishing treatment sample with Panel data in SAS

sas grouping panel-data

I have panel data that looks something like this: