【发布时间】: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