【问题标题】:Locate fragment view elements and update them after asynctask is done定位片段视图元素并在异步任务完成后更新它们
【发布时间】:2013-11-24 00:16:53
【问题描述】:

我有一个包含多个片段的活动。每个片段都实现了我的 UpdateRequest 接口,其中每个片段都执行一些异步任务并从 Web 服务下载数据。我的问题是我不太了解如何更新现有片段。我读了这个

Android Refreshing Fragment View after AsyncTask

但我仍然不知道如何定位每个片段的视图然后更新它们。我举个例子:

有一个 UserFragment,应该代表用户个人资料:

public class UserFragment extends SherlockFragment implements
        UpdateRequest {

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_user_info,
                container, false);
        return rootView;
    }
    ...
    public void update(final OnFragmentUpdatedListener context) {
    ...
    asyntask
     ...
    }

}

fragment_user_info 看起来像这样:

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



    <ImageView
        android:id="@+id/fragment_user_picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/image_of_user" />

    <TextView
        android:id="@+id/fragment_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/user_info" />

</RelativeLayout>

我想要的是找到 UserFragment 的实例,然后在其 AsyncTask.onPostExecute() 方法中将 fragment_user_name textView 中的文本更改为 asynctask 返回的内容。

【问题讨论】:

    标签: java android android-fragments android-asynctask


    【解决方案1】:

    最简单的方法是在片段中创建一个 final 变量来保存对 TextView 的引用。然后,作为片段的匿名内部类的 AsyncTask 将可以直接访问它。所以,是这样的:

    公共类 UserFragment 扩展 SherlockFragment 实现 UpdateRequest {

    private View rootView;
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_user_info, container, false);
        return rootView;
    }
    ...
    public void update(final OnFragmentUpdatedListener context) {
        final TextView userNameView = (TextView) rootView.findViewById(R.id.fragment_user_name);
        ...
        asyntask
        ...
        onPostExecute() {
            userNameView.setText(....);
        }
    
    }
    

    【讨论】:

    • findByID() 应该是 findViewById(int) ,它将返回对您要更新的 TextView 的引用。 OP 应该能够从 onPostExecute() 调用 findViewById()。 @GreyBeardedGeek,如果你修正了你的答案,我会投赞成票。
    • 我也在考虑这个解决方案,但是我如何确定在更新之前调用了 onCreateView 呢?然后 rootView 将为空,我们都知道这将如何结束。
    • 好的,我终于找到了合适的解决方案。我按照您的建议进行了操作,但这返回了 NullPointerException,因为正如我预期的那样,在 onCreateView 之前调用了 update()。所以我移动了“最终 TextView userNameView = (TextView) rootView.findViewById(R.id.fragment_user_name);”代码到 onPostCreate,现在它工作得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2023-04-02
    • 2016-09-30
    相关资源
    最近更新 更多