1

I have a simple ListView where each item is an EditText. However, whenever I try to start typing in one of the EditText's, it is immediately unfocused (leaving the soft keyboard up). Debugging shows that getView() is being called many times when I try to select the EditText.

In onCreate():

mList = new ArrayList<String>();
mList.add("Hello");
mList.add("Goodbye");

final ArrayAdapter<String> optionListAdapter = new EditTextAdapter(this, mList);

optionListView.setAdapter(optionListAdapter);

Custom Adapter:

private class EditTextAdapter extends ArrayAdapter<String> {
    private final Context mContext;
    private final ArrayList<String> mValues;

    public EditTextAdapter(Context context, ArrayList<String> list) {
        super(context, R.layout.edit_text_item, list);
        mContext = context;
        mValues = list;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.edit_text_item, parent, false);
        }
        final EditText editText = (EditText) rowView.findViewById(R.id.editText);
        editText.setText(mValues.get(position));
        editText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                mValues.set(position, editText.getText().toString());
                notifyDataSetChanged();
                return true;
            }
        });
        return rowView;
    }
}

edit_text_item.xml :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="Add Item Here"/>
</LinearLayout>
Peter Majeed
  • 5,304
  • 2
  • 32
  • 57
cstack
  • 2,152
  • 6
  • 28
  • 47

3 Answers3

2

Remove the following method call notifyDataSetChanged(); from onKey()

and add these parameters in your manifest file for the activity where you have used the list

android:windowSoftInputMode="adjustPan"

ravindra.kamble
  • 1,023
  • 3
  • 11
  • 20
  • This massively helped me. Thanks. See the answer here http://stackoverflow.com/questions/17410499/difference-between-adjustresize-and-adjustpan-in-android for more information about 'adjustPan' – spogebob92 Jan 18 '16 at 16:27
  • Thanks a lot, this seems to be the correct solution for the issue. – sahil shekhawat Apr 28 '16 at 20:34
1

In the onKey() method, you can simply call editText.requestFocus() after all the work is done (i.e. before the return statement).

---- EDIT ----

If it doesn't work, try adding these two lines after the editText.requestFocus() statement:

final InputMethodManager imm = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, 0);
nKn
  • 13,691
  • 9
  • 45
  • 62
  • This isn't the problem. The onKey() callback is not even being called. I just try to select the EditText, and getView() is called many times in a row. – cstack Jan 13 '14 at 05:18
0

You need to make it so that views created by the listview can contain focusable items.

In onCreate():

final ArrayAdapter<String> optionListAdapter = new EditTextAdapter(this, mList);
optionListAdapter.setItemsCanFocus(true);
optionListView.setAdapter(optionListAdapter);

And in your layout xml file add the following to the Listview declaration:

android:descendantFocusability="afterDescendants"
Lori
  • 1