Yassie
Yassie

Reputation: 326

Android EditText field works fine on emulator, but not device

I'm having a strange problem. What I did was to make a custom ListView adapter with TextViews and EditText fields. When I press a button, the app takes the EditText field values and saves them to the device's SD card.

My app works perfectly fine on the emulator, but the problem is that the EditText fields keep deselecting themselves when I test it on my device. In other words, I would click the EditText field, the keyboard and cursor would pop up, but the cursor would them immediately dissappear afterwards, making it impossible to input any text.

My custom adapter code:

@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
    if (view == null)
    {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        view = inflater.inflate(R.layout.list_item_task_detail, viewGroup, false);
    }

    final TaskDetailModel taskDetailModel = listArray.get(i);

    TextView textView = (TextView)view.findViewById(R.id.list_item_task_detail_text_view);
    textView.setText(taskDetailModel.getTaskName());

    EditText editText = (EditText)view.findViewById(R.id.list_item_task_detail_edit_text);

    return view;
}

The EditText layout entry:

<EditText
    android:id="@+id/list_item_task_detail_edit_text"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

Is there some kind of editable = true I need to add to the EditText field? I feel that I'm making a mistake somewhere that I can't seem to find. I also just don't understand why the app works on the emulator and not on my real phone.

BTW the emulator and my phone use the same SDK version, API level 19 (4.4.x)

Upvotes: 0

Views: 511

Answers (1)

Yassie
Yassie

Reputation: 326

I think I managed to fix my own problem...

All I needed to do was to add the line

editText.requestFocus()

to my adapter.

I'm guessing that the button's click listener overrides the EditText field's focus request if requestFocus() is not set for some strange reason. Still don't know why this wasn't a problem on the emulator though

Upvotes: 0

Related Questions