Android 从片段获取异步任务结果数据
我有一个名为LoadXMLData的AsyncTask类,您可以看到,我在doInBackground()方法中解析XML数据Android 从片段获取异步任务结果数据,android,android-fragments,android-asynctask,Android,Android Fragments,Android Asynctask,我有一个名为LoadXMLData的AsyncTask类,您可以看到,我在doInBackground()方法中解析XML数据 public class LoadXMLData extends AsyncTask<String, RSSFeed, RSSFeed>{ public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private ProgressDialog mProgressDialog; private Conte
public class LoadXMLData extends AsyncTask<String, RSSFeed, RSSFeed>{
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
private Context context;
RSSFeed feed;
public LoadXMLData(Context context) {
this.context = context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Molimo Vas, sačekajte. Podaci se učitavaju.");
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
Log.d("OVDE SAM:", "onPreExecute()");
}
@Override
protected RSSFeed doInBackground(String... urls) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(urls[0]);
Log.d("OVDE SAM:", "PARSIRAM XML");
return feed;
}
@Override
protected void onPostExecute(RSSFeed result) {
mProgressDialog.dismiss();
super.onPostExecute(result);
}
public类LoadXMLData扩展异步任务{
公共静态最终整型对话框\u下载\u进度=0;
private ProgressDialog mProgressDialog;
私人语境;
RSSFeed饲料;
公共LoadXMLData(上下文){
this.context=上下文;
mProgressDialog=新建进度对话框(上下文);
mProgressDialog.setMessage(“Molimo Vas,sačekajte.Podaci se učitavaju.”);
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
mProgressDialog.show();
Log.d(“OVDE SAM:,“onPreExecute()”);
}
@凌驾
受保护的RSSFeed doInBackground(字符串…URL){
//获取饲料
DOMParser myParser=新的DOMParser();
feed=myParser.parseXml(URL[0]);
Log.d(“OVDE SAM:,“PARSIRAM XML”);
回馈;
}
@凌驾
受保护的void onPostExecute(RSSFeed结果){
mProgressDialog.disclose();
super.onPostExecute(结果);
}
}
我有几个片段,需要从异步任务中获取数据。我怎么能做到
下面是一个名为NajnovijeFragment的片段的代码
public class NajnovijeFragment extends Fragment{
GridView lv;
RSSFeed feed;
CustomListAdapter adapter;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_najnovije, container,
false);
lv = (GridView) view.findViewById(R.id.GridView1);
// Set an Adapter to the ListView
adapter = new CustomListAdapter();
lv.setAdapter(adapter);
// Set on item click listener to the ListView
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// actions to be performed when a list item clicked
int pos = arg2;
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtras(bundle);
intent.putExtra("pos", pos);
startActivity(intent);
}
});
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public CustomListAdapter() {
layoutInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(getActivity().getApplicationContext());
}
public int getCount() {
// TODO Auto-generated method stub
// Set the total list item count
return feed.getItemCount();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
TextView tvDate = (TextView) listItem.findViewById(R.id.tvDate);
// Set the views in the layout
imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
tvTitle.setText(feed.getItem(pos).getTitle());
tvDate.setText(feed.getItem(pos).getDate());
return listItem;
}
}
公共类NajnovijeFragment扩展了片段{
GridView lv;
RSSFeed饲料;
自定义列表适配器;
专用字符串RSSFEEDURL=”http://balkanandroid.com/feed/";
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment_najnovije,容器,
假);
lv=(GridView)view.findViewById(R.id.GridView1);
//将适配器设置为ListView
adapter=新的CustomListAdapter();
低压设置适配器(适配器);
//在项目上设置单击ListView的侦听器
lv.setOnItemClickListener(新的OnItemClickListener(){
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
//单击列表项时要执行的操作
int pos=arg2;
Bundle=新Bundle();
bundle.putSerializable(“提要”,提要);
Intent Intent=new Intent(getActivity(),DetailsActivity.class);
意向。额外支出(捆绑);
意向。额外(“pos”,pos);
星触觉(意向);
}
});
返回视图;
}
@凌驾
公共空间{
super.ondestory();
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
类CustomListAdapter扩展了BaseAdapter{
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
公共图像加载器;
公共CustomListAdapter(){
LayoutFlater=(LayoutFlater)getActivity().getSystemService(
上下文。布局(充气机和服务);
imageLoader=新的imageLoader(getActivity().getApplicationContext());
}
public int getCount(){
//TODO自动生成的方法存根
//设置总列表项计数
返回feed.getItemCount();
}
公共对象getItem(int arg0){
//TODO自动生成的方法存根
返回null;
}
公共长getItemId(int arg0){
//TODO自动生成的方法存根
返回0;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
//充气项目布局并设置视图
查看列表项=转换视图;
int pos=位置;
如果(listItem==null){
listItem=LayoutFlater.inflate(R.layout.list\u项,空);
}
//初始化布局中的视图
ImageView iv=(ImageView)listItem.findViewById(R.id.thumb);
TextView tvTitle=(TextView)listItem.findViewById(R.id.title);
TextView-tvDate=(TextView)listItem.findViewById(R.id.tvDate);
//在布局中设置视图
imageLoader.DisplayImage(feed.getItem(pos.getImage(),iv);
setText(feed.getItem(pos.getTitle());
tvDate.setText(feed.getItem(pos.getDate());
返回列表项;
}
}
}AsyncTask是一个与UI非常相关的类,如果需要使用此XML解析更新UI,则应考虑以下事项:
- 使asynctask成为片段中的内部类或
- 将片段传递给异步任务
- 在onPostExecute()中更新片段的视图
onPostExecute(Object xml) {
if(getActivity != null) {
// update Views like...
textViewLabel.setText(parsedXml.getTitle);
}
}
我建议您使用SafeAncyncTask,它是Roboguice项目中的一个java类,只有一个文件,它与java.util.concurrent.Callable相关,只需复制并粘贴源代码:
AsyncTask是一个与UI非常相关的类,如果需要使用此XML解析更新UI,则应考虑以下事项:
- 使asynctask成为片段中的内部类或
- 将片段传递给异步任务
- 在onPostExecute()中更新片段的视图
onPostExecute(Object xml) {
if(getActivity != null) {
// update Views like...
textViewLabel.setText(parsedXml.getTitle);
}
}
我建议您使用SafeAncyncTask,它是Roboguice项目中的一个java类,只有一个文件,它与java.util.concurrent.Callable相关,只需复制并粘贴源代码:
您的AsyncTask已经知道了上下文,所以您可以在onPostExecute中调用您的活动(为了说明起见称为ActivityMain)。e、 g
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
((ActivityMain) context).loadCompleteHandler(param1,param2,...)
}
然后,您将决定如何在活动中实现loadCompleteHandler。现在您的活动可能不存在,因此在删除活动时必须小心取消异步任务。属于某个活动的片段也可以访问该活动。您的AsyncTask已经知道了上下文,因此您可以在onPostExecute中调用您的活动(为了说明起见称为ActivityMain)。e、 g
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
((ActivityMain) context).loadCompleteHandler(param1,param2,...)
}
public class NajnovijeFragment extends Fragment implements OnXMLLoadFinishedListener{
@override
public void onXMLDataReady(RSSFeed results){
//display your data.
}
LoadXMLData xmlLoader = new LoadXMLData();
xmlLoader.setOnXMLLoadFinishedListener(this);