ListView刷新主题



上周有个问题一直困扰着我。我有一个listView与一个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);
}

这是活动xml文件

<?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>

问题是,当我改变的ArrayAdapter的内容listView不反映的变化。

下面是改变ArrayAdapter

的click事件代码
public void onSendMeClick(View view)
{
    if(chkSendMe.isChecked())
    {
        SelectedUsersAdapter.clear();
        SelectedUsersAdapter.add("test String");
        SelectedUsersAdapter.notifyDataSetChanged();
    }
    else
    {
        SelectedUsersAdapter.clear(); 
        SelectedUsersAdapter.notifyDataSetChanged();
    }

当我更新到Android sdk tools 23.0.2和build tools 20时,一切都开始了。在以前的sdk工具中没有问题(22.6)。更糟糕的是,有时(很少)问题会消失(列表正确地更改其内容)。但是,如果我触摸editText字段和虚拟键盘出现,那么当我关闭键盘时,列表反映了ArrayAdapter中的每一个变化。那我就永远没有问题了。我试图使一切都无效。什么都没有。问题就在那里。我还更改了整个xml文件,并将所有内容放在RelativeLayout下。没有什么了。当然没有错误被显示出来。我的代码有什么问题?

解决方案。我找到了问题所在。我把它写在这里,以防有人遇到这样的问题。我在MainActivity功能的服务引用是静态的,所做的就是在主视图的textview中写一段文字。但我注意到服务调用了视图。主视图的RequestLayout。所以我认为这就像是服务控制了主视图。当我改变它从UI线程运行一切都是正常的。谢谢大家。

在为列表视图设置适配器之前是否尝试过SelectedUsersAdapter.setNotifyOnDataChanged(true); ?这是我一直用的,从来没有出过问题。

ArrayAdapter.setNotifyOnChange(布尔)

编辑:notifyDataSetChanged()仅通知ListView数据已更改,而不是重新绘制自身。

请尝试在更改数据后添加refreshDrawableState。

最新更新