Android ArrayAdapter/ListView 不会在 Fragment 中更新

标签 android android-listview android-fragments android-linearlayout android-arrayadapter

我在通过 Android Fragment 中的 ArrayAdapter 更新 ListView 时遇到问题。

我有一个处理从服务器检索的 .csv 的方法。它解析 .csv 并执行以下操作:

for(int i =0; i< lines.length; i++){
    String[] words = lines[i].split(",");
        for(int j = 0;j<words.length; j++)
            if ( j%6 == 1 || j%6 == 2)
                getArrayAdapter().add(words[j]);

使用断点,我可以看到“words[j]”是一个有效的字符串。 getArrayAdapter 方法如下:

private ArrayAdapter<String> getArrayAdapter(){
   if (aa == null){
        ListView lv = (ListView)   getCurrentActivity().findViewById(R.id.list);
        aa = new ArrayAdapter<String>(
                getCurrentActivity(),
                android.R.layout.simple_list_item_1);
        aa.setNotifyOnChange(true);
        lv.setAdapter(aa);
    }
    return aa;
}

如果“getArrayAdapter().add(words[j]);”被调用时,ArrayAdapter 中的方法“notifyDataSetChanged()”被调用,因为它应该:

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    mNotifyOnChange = true;
}

通过它的父级,它到达执行此操作的 DataBaseObservable:

public void notifyChanged() {
    synchronized(mObservers) {
        // since onChanged() is implemented by the app, it could do anything, including
        // removing itself from {@link mObservers} - and that could cause problems if
        // an iterator is used on the ArrayList {@link mObservers}.
        // to avoid such problems, just march thru the list in the reverse order.
        for (int i = mObservers.size() - 1; i >= 0; i--) {
            mObservers.get(i).onChanged();
        }
    }
}

观察者数组有一个“AdapterDataSetObserver”,其中包含对我的适配器和 ListView 的所有类型的引用。我查看了 AdapterDataSetObserver 的 onChanged() 中发生的情况,但我真的不知道要查看什么。

我的 table_fragment.xml 看起来像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  android:id="@+id/top">
    <ListView android:layout_width="match_parent"
              android:layout_height="0dip"
              android:layout_weight="2"
              android:numColumns="2"
              android:columnWidth="30dp"
              android:id="@+id/list">
    </ListView>
</LinearLayout>

包含 fragment 的 Activity 的 xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <fragment android:name="com.example.TableFragment"
              android:id="@+id/table_fragment"
              android:layout_weight="0"
              android:layout_width="match_parent"
              android:layout_height="0dip" />
</LinearLayout>

查看日志,没有什么奇怪的地方。一切似乎都很顺利,但屏幕上没有显示任何字符串。

我在这里错过了什么?可能遗漏了一些微不足道的东西,因为我是 Android 的新手。

最佳答案

android:layout_height="0dip"

这是错误的。您应该让 ListView 获得所有可用空间。在 fill_parent

中更改它

然后:

aa = new ArrayAdapter<String>(
                getCurrentActivity(),
                android.R.layout.simple_list_item_1);

您需要将要显示的数据集提交给 Apdater。为此,您应该使用此 constructor

关于Android ArrayAdapter/ListView 不会在 Fragment 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12582208/

相关文章:

java - 如何更改以编程方式添加的图像大小

android - 没有从 EditText 得到任何东西

java - 选项卡不填充 tablayout

android - 带正则表达式的过滤器适配器

android - 收到后台服务的响应后刷新 Activity

android - fragment 父布局透明

android - 获取包括字体大小的文本高度并设置该高度

android - ArrayAdapter 中 ListView 第一行的不同布局

android - 从 viewPager 中的 fragment 隐藏父 fragment

android - 切换 fragment 错误