Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText Loses Focus on Click

I'm writing an activity to list a bunch of items from the database and show an additional column with a quantity. I implemented an ArrayAdapter to show my custom layout. Here is the code of the most important parts of the Adapter:

static class ViewHolder {
    protected TextView text;
    protected EditText editText;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.rowquantitylayout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.editText = (EditText) view.findViewById(R.id.editText);
        viewHolder.editText.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Log.i(TAG, "Editable:" + s.toString());
            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
            }
        });
        view.setTag(viewHolder);
        viewHolder.editText.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).editText.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getDescription());
    holder.editText.setText("" + list.get(position).getQuantity());
    return view;

}

Now, when I try to focus the "EditText" it loses focus just after I clicked it. It shows the keboard and it even gets to the Log.i, but doesnt let me change the value since it is not focused.

Thanks for the help

like image 681
Kohakukun Avatar asked Feb 16 '13 12:02

Kohakukun


1 Answers

I had the same problem and I searched for an answer. Here is an answer to a similar question : https://stackoverflow.com/a/9338694/1448661

You have to put a line in your activity in the manifest :

    android:windowSoftInputMode="adjustPan"

And your listview must have this line too :

    android:descendantFocusability="beforeDescendants"

And now you will probably face another problem which is the lost of your text when scrolling. If this happens take a look at this tutorial : http://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/

Hope this will help. Goui

like image 150
guillaumegoui Avatar answered Sep 18 '22 22:09

guillaumegoui