Stack Exchange
ListView refresh subject
I have a problem that bothers me for the last week. I have a listView that is connected with an ArrayAdapter
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    chkSendMe=(CheckBox)findViewById(R.id.chkSendMe);
    lv = (ListView) findViewById(R.id.outputList);
    res = getResources();
    SelectedUsersAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1);
    lv.setAdapter(SelectedUsersAdapter);
}

Here is the activity xml file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/TopLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:paddingBottom="4dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <Button
        android:id="@+id/btnSelectUsers"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:onClick="ShowSelection"
        android:text="@string/select_recipients" />

    <CheckBox
        android:id="@+id/chkSendMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/btnSelectUsers"
        android:onClick="onSendMeClick"
        android:text="@string/send_myself"
        android:textSize="13sp" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/ListLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="2dp" >

    <ListView
        android:id="@+id/outputList"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:textSize="12sp" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/MessageLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:paddingBottom="1dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="1dp" >

    <TextView
        android:id="@+id/lblMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="17dp"
        android:text="@string/w_message" />

    <EditText
        android:id="@+id/txtMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btnEmoticon"
        android:ems="10"
        android:height="70dp"
        android:inputType="textMultiLine"
        android:maxHeight="100dp"
        android:scrollbars="vertical"
        android:textSize="12sp" >

        <requestFocus />
    </EditText>

    <ImageButton
        android:id="@+id/btnEmoticon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignRight="@+id/txtMessage"
        android:layout_alignTop="@+id/lblMessage"
        android:layout_marginRight="16dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/emoticonButton"
        android:cropToPadding="false"
        android:onClick="OnbtnEmoticonsClick"
        android:src="@drawable/emoticons01" />

    <ImageButton
        android:id="@+id/btnImportDoc"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentTop="true"
        android:layout_marginRight="24dp"
        android:layout_toLeftOf="@+id/btnEmoticon"
        android:background="@android:color/transparent"
        android:contentDescription="@string/import_doc_button"
        android:cropToPadding="false"
        android:onClick="onbtnImportDocClick"
        android:src="@drawable/attach_icon" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/BottomLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.41"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:paddingBottom="0dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin" >

    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btnServiceHandle"
        android:layout_alignParentTop="true"
        android:minHeight="35dp"
        android:onClick="onSendClick"
        android:text="@string/send"
        android:textSize="13sp" />

    <ToggleButton
        android:id="@+id/btnServiceHandle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:minHeight="50dp"
        android:onClick="onServiceToggleClicked"
        android:text="@string/service_handle"
        android:textOff="@string/service_handle_off"
        android:textOn="@string/service_handle_on"
        android:textSize="12sp" />

</RelativeLayout>

</LinearLayout>

The problem is that when I change the contents of the ArrayAdapter the listView does not reflect the changes.
Below is the code of the click event that changes the ArrayAdapter
public void onSendMeClick(View view)
{
    if(chkSendMe.isChecked())
    {
        SelectedUsersAdapter.clear();
        SelectedUsersAdapter.add("test String");
        SelectedUsersAdapter.notifyDataSetChanged();
    }
    else
    {
        SelectedUsersAdapter.clear(); 
        SelectedUsersAdapter.notifyDataSetChanged();
    }

It all started when I updated to Android sdk tools 23.0.2 and build tools 20. In previous sdk tools there was no problem (22.6).
To make things worse sometimes (rarely) the problem disappears (the list changes its contents properly). 
But if I touch the editText field and the virtual keyboard appears then the list is reflecting every change in ArrayAdapter when I close the the keyboard. Then I have no problem permanently.
I have tried to invalidate everything. Nothing. The problem is there. I also changed the whole xml file and put everything under a RelativeLayout. Nothing again. And of course no error is manifested.
What is wrong with my code?
Solution. I found the problem. I write it here just in case someone comes across with something like this. I had in service a reference in a MainActivity function that was static and all that was doing was to write a piece of text in a textview in main view. But I noticed that the service was calling view.RequestLayout of the main view. So I think it was like the service taking control of the main view. When I changed it to run from UI thread everything is normal.
Thank you all.

Have you tried SelectedUsersAdapter.setNotifyOnDataChanged(true); before you set the adapter for the list view.  This is what I've always used and never had a problem with it.
ArrayAdapter.setNotifyOnChange(boolean)
Edit:
notifyDataSetChanged() only notifies ListView that data has changed, not to redraw itself.  Try adding refreshDrawableState after you've changed the data.

I did try but the problem is still present. The adapter does change its items but the command lv.getChildCount() does not change from 0 until I press the editText and the virtual keyboard comes forward. This means that the problem is in the listview

Right!  Forgot about ListView needing to redraw itself.  Updated my answer.

I've already did but with no success. I 've tried every invalidate function there is.

Something new I didn't know before. In onCreate() function I start a service. When I start the service with the usual command "startService(service_intent);" the listview has problem. When I comment out this command the list view refreshes its self appropriately. Does this designate that the view looses focus or something and if yes has anyone stumble on something like this?

Does the service interact with the listview at all?
