20

I've got a problem with my EditText. I use the following adapter:

public class RowTextViewAdapter extends BaseAdapter {

...

  public View getView(int position, View convertView, ViewGroup parent) {

  ViewHolder holder = null;

    if (rowTitles.get(position).equals("edit")) {
        if(et == null){
            et = new EditText(activity);
            et.setText("Test");
        }
        return et;
    }
    else {
        convertView = new TextRow(activity);
        holder = new ViewHolder(((TextRow) convertView).getTextView(), ((TextRow) convertView).getImageView());
        convertView.setTag(holder);
        holder.getTextView().setText(StringManager.getInstance().getText(rowTitles.get(position), activity));
        holder.getImageView().setImageBitmap(assetController.getBitmap(additiveIcons.get(position) + ".png", null));
        return convertView;
    }
  }
}

and ListActivity:

public class AppSettingActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        adapter = new RowTextViewAdapter(this);
        adapter.addRowView("account", "arrowDw");
        adapter.addRowView("password", "arrowDw");
        setListAdapter(adapter);
   }

...

   protected void onListItemClick(ListView listView, View view, int position, long id) {
    switch (position) {
        case 0: accIsEditable = adapter.setEditable(position); break;
        case 1: 
            if(accIsEditable) {
                                    //TODO do something..
                break;
            }
            pwIsEditable = adapter.setEditable(position);
            break;
          ...
   }
}

If i click on the first item I add a new list item on pos. 1 (pos: 0, 1, 2, ...). Now the EditText field is added to the list.

ListView:
----------------------------          -------------------------    
Account                   v           Account                ^     
----------------------------    ==>   -------------------------     
Passowrd                  v           [::::::::EditText:::::::]      
----------------------------          -------------------------
//more..                              Password               v
----------------------------          -------------------------
                                      //more..
                                      -------------------------

If I click now into the EditText field, it shows the virtual keyboard and loses focus of the EditText. I click again and it gains focus. But if I write something, the text is only showed in the EditText field, if i tap on it and not frequently while i'm writing...

Any idea to fix that update problem?

Javatar
  • 245
  • 1
  • 2
  • 8
  • This sounds remarkably like this issue: http://stackoverflow.com/questions/6918494/edit-text-in-listactivity-listview-looses-focus-when-keyboard-comes-up Hope it helps :-) – atraudes Nov 28 '11 at 06:07
  • Well, i got those focus problems too. I tried it out with: `addTextChangedListener` -> Debugging is correct - shows right text. – Javatar Nov 28 '11 at 14:02
  • But I can't figure out howto set the text (`EditText.setText("new listeners text")`) without an exception. I Hadn't found a method for set the FOCUS again on the EditText. The only thing I can do is to call `getInstance().notifyDataSetChanged()` but after that I always have to click manually into the EditText-field again and it writes only one letter.. With the `EditText.requestFocus()` the keyboard isn't showing up and the EditText is no more focusable – Javatar Nov 28 '11 at 14:08
  • Btw strange thing: the cursor in the EditText isn't blinking... its like the EditText is freezed. :S – Javatar Nov 28 '11 at 15:07
  • 1
    Don't put a EditText into a ListView. It sounds like you could swap your listView for a LinearLayout in a ScrollView! :D – Joe Simpson Nov 28 '11 at 15:18
  • It's a kind of Dropdown.. **TableRows** (Text + ImgView) + **EditTexts** added with BaseAdapter to a ListActivity – Javatar Nov 28 '11 at 17:04
  • Edittext and listview dont generally gel well. I agree with joe's answer. Also there might be a problem if you have a long list and if its suppose to resize. Also there is this requestFocusOnTouch not sure if it would make a difference but worth trying. – Jayshil Dave Jan 30 '12 at 18:23
  • Possible duplicate of [Focusable EditText inside ListView](http://stackoverflow.com/questions/2679948/focusable-edittext-inside-listview) – blahdiblah Nov 01 '13 at 01:07

2 Answers2

39

If this is still an issue, take a look at this item: Focusable EditText inside ListView

This changes may help.

Change to listview:

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

Change to activity in mainfest.xml:

<activity android:name= ".yourActivity" android:windowSoftInputMode="adjustPan"/>
Community
  • 1
  • 1
eyespyus
  • 1,576
  • 19
  • 20
  • Thanks, but I ran out of time so... rejected – Javatar May 17 '12 at 15:34
  • 8
    `android:windowSoftInputMode="adjustPan"` alone works for me. – Pang Apr 17 '13 at 06:58
  • Perfect! Thanks much! I'll have to explore what each of those settings does, but I have no doubt you just saved me hours of running in circles... – Todd Grigsby Apr 25 '13 at 14:07
  • 1
    Thank you so much. Now my listview is finally working like it should. I'm setting the value programmatically via: `myActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);`. – RhodanV5500 Nov 08 '13 at 10:25
  • 3
    It had to be afterDecendants for me. android:descendantFocusability="afterDescendants" – Dittimon Apr 22 '14 at 04:34
  • My situation is the same as Dittimon, I had to set it to "afterDescendants" and then the editText would take focus like I wanted. – Phil Ringsmuth Apr 03 '15 at 00:48
1

some times when you use android:windowSoftInputMode="stateAlwaysHidden"in manifest activity or xml, that time it will lose keyboard focus. So first check for that property in your xml and manifest,if it is there just remove it. After add these option to manifest file in side activity android:windowSoftInputMode="adjustPan"and add this property to listview in xml android:descendantFocusability="beforeDescendants"