【问题标题】:Android: ListFragment: How to refresh listAndroid:ListFragment:如何刷新列表
【发布时间】:2012-11-19 19:10:27
【问题描述】:

我有一个显示在片段上的列表视图。我在屏幕底部有一个按钮,按下该按钮时,将调用 Web 服务来获取任何其他数据。如果有其他数据,我需要将其添加到列表视图中。我已经搜索了这个论坛和许多其他网站,试图找到如何做到这一点,但我没有成功。任何帮助深表感谢。

我现在正在考虑是否需要动态添加片段,而不是在以下 XML 布局中定义它。

我正在使用 ListFragment 来扩充屏幕上的列表视图。

我有一个屏幕,上面有两个片段。屏幕的 XML 如下:-

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <fragment
            android:name="atos.mobilereporting.com.ReportList"
            android:layout_width="323dp"
            android:layout_height="match_parent" />

        <fragment
            android:name="atos.mobilereporting.com.ReportDetail"
            android:layout_width="958dp"
            android:layout_height="match_parent" />
    </LinearLayout>


    <Button
        android:id="@+id/getReports"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Refresh Mobile Reports" />

</LinearLayout>

膨胀视图的代码如下:-

 public class ReportList extends ListFragment {

      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Get list of reports...
            repList = ReportDefinitionFactory.buildReportList(3);

            activity = getActivity();

            ListAdapter la = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1,
                    ReportDefinitionFactory.getReportDescriptions(repList));

            setListAdapter(la);

        }

    }

这段代码显示了一个简单的列表视图,上面有 3 行。

此时我必须停下来,因为我不知道从这里出发。我的代码将向用于最初构建列表视图的数组添加额外的行,但我不知道如何在列表视图上调用刷新。

谢谢

马丁

【问题讨论】:

    标签: android android-listfragment


    【解决方案1】:

    一旦数据发生变化,您需要调用la.notifyDataSetChanged() 强制刷新列表。

    【讨论】:

    • 嗨,它说方法 notifyDataSetChanged 对于 ListAdapter 类型未定义。
    • 只需将'ListAdapter la = new ArrayAdapter'替换为'ArrayAdapter la = new ArrayAdapter'
    • 事情看起来好多了,我现在意识到,我没有正确填充我的数组。感谢您提供建议。
    【解决方案2】:

    使用 ArrayAdapter notifyDataSetChanged() 函数。

    这可以添加到 ArrayAdapter 中,如果那是更新数据的位置。否则,将其添加到调用la.add() 的同一区域。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 2015-07-18
      • 2014-10-09
      相关资源
      最近更新 更多