Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: EditText in ListView issue

Tags:

java

android

I have a listview in my application that is basically a questionaire so there are some EditTexts that the user needs to fill. I've encountered the following issues:

  1. Some of the EditText require numeric input, so I set the inputType in the corresponding xml type as numeric, however, as when I click on the EditText the numeric keyboard is shown but almost immediately it disappears and the regular keyboard is shown.

  2. As you can imagine, I need to store the values that the user inputs in these EditTexts. The problem I have with this is that after I input some text in SOME EditTexts and I scroll down, the text is gone when I go back. You can see in my code that I made an attempt to prevent this and I should mention that it works perfectly fine with checkboxes.

  3. At the beginning I was having problems with losing the focus but I solved it looking at other questions (added descendantFocusablity="beforeDescendants" to the ListView and windowSoftInputMode="adjustPan"). Works fine except that when an EditText is below the half of the screen it loses focus as soon as I start typing into it. getView method for list adapter:

    public View getView(final int position,View result,ViewGroup parent)
    {
    final ViewHolder vh;
    final Question question = values.get(position);
    if(holders[position]==null)
        vh = new ViewHolder();
    else
        vh = holders[position];
    
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(question.getQuestionType().equals(Question.TYPE_CLOSED))
    {
        result = inflater.inflate(R.layout.closed_question_list_item, null);
        vh.cb  = (CheckBox)result.findViewById(R.id.checkbox);
        vh.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {
                vh.isChecked      = isChecked;
                holders[position] = vh;
            }
        });
        vh.cb.setChecked(vh.isChecked);
    }
    
    else
    {
        result = inflater.inflate(R.layout.numeric_question_list_item, null);
        vh.et  = (EditText)result.findViewById(R.id.question_et);
        vh.et.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void afterTextChanged(Editable s) 
            {
                vh.tvValue = s.toString();
                holders[position] = vh;
                Log.i(TAG,"Entered afterTextChanged for "+ question.getText());
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start,int count, int after) 
            {   
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start,int before, int count) 
            {
            }
        });
        vh.et.setText(vh.tvValue);
    }
    vh.tv = (TextView)result.findViewById(R.id.question_text);
    
    vh.tv.setText(question.getText());
    
    holders[position] = vh;
    result.setTag(vh);
    return result;
    }
    
like image 455
user2535545 Avatar asked Jun 30 '13 00:06

user2535545


1 Answers

To expand on aaRBiyecH's answer above, here is a solution and a more in-depth explanation to your questions #1 and #3 (source):

You need to set the DescendantFocusability property of your ListView to "afterDescendants".

You can do this in the XML like this:

android:descendantFocusability="afterDescendants"

or in code like this:

listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS)

You also need to set the items to be focusable.

You can do this in the code like this: listview.setItemsCanFocus(true);

Also, you need to specify what is happening to the main window of your activity when the soft keyboard is shown. This is done by changing the WindowSoftInputMode attribute. You probably want to set it to AdjustPan so that your listview is not resized and thus GetView isn't called again. This is probably what is happening now when you hide the keyboard, GetView is called again and reset the original value in the EditText.

You can do this using the attribute android:windowSoftInputMode="adjustPan" on your main activity class's XML, or getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_PAN) programmatically.

like image 118
matt Avatar answered Sep 21 '22 17:09

matt