I'm trying to create a ListView
(with custom CursorAdapter
) of EditText
items such that EditTexts
appear uneditable at first and become editable upon long click. Then the user would edit EditText
contents and EditText
would save changes to db upon losing focus. However I've run into a really nasty behaviour that prevents me from doing this.
1) I've set my EditTexts
to android:focusable="false"
and android:focusableInTouchMode="false"
in XML.
2) I've created an OnItemLongClickListener
in my ListActivity
that does the following:
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
Log.d("NLAc", "longClick called");
EditText et = (EditText) view.findViewById(R.id.etFolderName);
et.setFocusable(true);
et.setFocusableInTouchMode(true);
return true;
}
3) And when I create views in my adapter I attach the following focus change listener:
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
EditText et = (EditText)v;
Log.d(TAG, "hasFocus true called " + et.getText());
et.setText("focused");
et.setSelection(et.length());
}
else
{
EditText et = (EditText)v;
Log.d(TAG, "hasFocus false called " + et.getText());
et.setText("unfocused");
et.setFocusableInTouchMode(false);
//TODO Save to DB
}
}
What happens is that when I long click the very first item I get the following in the log:
longClick called
hasFocus true called item1
hasFocus false called focused
If I remove the line setting focusable to false (et.setFocusableInTouchMode(false);
) I get another hasFocus true called unfocused
.
Apparently things go like this:
1) EditText
gets focus when set focusable
2) LinearLayour
containing my ListView
loses focus and calls internal unFocus()
on all it's children including my EditText
3) EditText
loses focus
4) EditText
gets focus - for whatever reason now.
This behaviour prevents me from disabling EditText
upon losing focus or making it unfocusable until the next long click comes through which is what I want.
Can anyone suggest what I may be missing? Or explain this behaviour? Any solutions appreciated.