1

我在通过 Android 片段中的 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 的活动的 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 的新手。

4

3 回答 3

2
android:layout_height="0dip"

这是错误的。你应该让ListView所有可用的空间。改进去fill_parent

然后:

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

您需要向 Apdater 提交要显示的数据集。为此,您应该使用此构造函数

于 2012-09-25T11:46:22.060 回答
0

事实证明,ListView 的 layout_height 可以为 0(它会自动扩展)。但是,在包含该片段的 Activity 的 xml 中,layout_height 无法设置为 0(它不会自动展开)。我也把它放在了 0dip 上(在 Eclipse 的建议之后)。更改为“match_parent”完成了这项工作(尽管有 Eclipse 警告)。

于 2012-09-25T13:44:20.193 回答
0

@Jos,您选择的答案不是正确答案。高度可以而且ListView应该是 0dp。为了使ListViewto 以默认方式运行,您需要将 Id 更改ListView为完全android:id="@id/android:list",您还可以添加一个TextVieworProgressBar以在列表为空时显示,也是 的默认功能ListView,如下所示:

<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@+string/empty_list_message" />

同样,id 必须完全匹配,其他所有内容都可以根据您的需要进行更改。这是完整的 xml 的样子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" 
        android:listSelector="@drawable/list_selector"/>

    <TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_list_message" />

</LinearLayout>

layout_weight和是我的程序所特有的listSelector,您可能需要也可能不需要这些。

于 2013-01-25T21:48:39.950 回答