外部AsyncTask的填充的ListView从片段 [英] External AsyncTask populating ListView from Fragment

查看:191
本文介绍了外部AsyncTask的填充的ListView从片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序基于关闭片段。很多这些片段的内容是从数据库中收集利用的的AsyncTask 。因此我想外部化获取数据类,因此它可以被重复使用。我的片段如下:

I am developing an app based off fragments. A lot of the content of these fragments is collected from a database utilizing an AsyncTask. As such I'm trying to externalize the 'getting data' class so it can be reused. My fragment is as follows:

public class LocalFragment extends SherlockListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        LoadDataFromURL url_data = new LoadDataFromURL();
        url_data.setContext(getSherlockActivity());
        url_data.setURL("http://url.com/get_data/");
        url_data.execute();

    }

} 

我的 LoadDataFromURL 类如下:

class LoadDataFromURL extends AsyncTask<String, String, String>{

    String our_url;
    ListActivity our_context;
    private ProgressDialog pDialog;
    JSONParser jParser = new JSONParser();
    JSONArray results = null;
    List<PubListDetails> pubs = new ArrayList<PubListDetails>();
    Handler mHandler;

    public void setContext(ListActivity context){
        our_context = context;
    }

    public void setURL(String url){
        our_url = url;
    }

    ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onPreExecute() {

        // TODO Auto-generated method stub
        super.onPreExecute();

        mHandler = new Handler();

        pDialog = new ProgressDialog(our_context);
        pDialog.setMessage("Loading pubs please wait..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONArray json = jParser.makeHttpRequest(our_url, "GET", params);


        Log.d("All Products: ", json.toString());


        try {
            int success = json.length();

            if (success != 0){
                results = json;

                pubs.clear();


                for (int i = 0; i <results.length(); i++){
                    JSONObject c = results.getJSONObject(i);

                    String id = c.getString("id");
                    String name = c.getString("name");
                    String town = c.getString("town");
                    String county = c.getString("county");

                    //HashMap<String, String> map = new HashMap<String, String>();


                    pubs.add(new PubListDetails(id,name,town,county));
                    //map.put(TAG_PID, id);
                    //map.put(TAG_NAME, name);

                    //productsList.add(map);
                }
                }else{
                    Intent i = new Intent(our_context,MainMenu.class);

                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    our_context.startActivity(i);
                }
            } catch (JSONException e){
                e.printStackTrace();
                }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(our_context, "ff", Toast.LENGTH_SHORT).show();
        // TODO Auto-generated method stub
        pDialog.dismiss();

our_context.runOnUiThread(new Runnable(){

    @Override
    public void run() {
        // TODO Auto-generated method stub
        ListAdapter adapter = new PubListAdapter(our_context, pubs);
        our_context.setListAdapter(adapter);
    }});

    }   

}

这是给我的错误的基础上的 getSherlockActivity 不通过一个 ListActivity ,其中 setListAdapter 要求。

It is giving me errors on the basis that getSherlockActivity does not pass a ListActivity, which setListAdapter requires.

这个独立,我感觉越来越多,我已经从概念上忽略了一点,这是不是达到我想要的理想方式。

Independent of this, I am feeling more and more that I have conceptually missed the point and this isn't the ideal way to achieve what I want.

可能有人告知如何从一个片段,我可以调用外部的AsyncTask 将收集数据,然后填充的ListView 呢?

Could someone advise how from a fragment I can call an external AsyncTask which will collect data and then populate a ListView with it?

推荐答案

首先,你要设置的数据上的 SherlockListFragment 让您的企业它不是用活动

First, you want to set the data on the SherlockListFragment so your business it's not with the Activity .

这个独立,我感觉越来越多,我有   从概念上忽略了一点,这是不是达到了理想的方式   我想要什么。

Independent of this, I am feeling more and more that I have conceptually missed the point and this isn't the ideal way to achieve what I want.

可能有人告知如何从一个片段,我可以调用外部   AsyncTask的将收集数据,然后填充列表视图   它..?

Could someone advise how from a fragment I can call an external AsyncTask which will collect data and then populate a list view with it..?

简单实现一个回调接口。

Simply implement a callback interface.

public interface OnLoadDataListener {
      public onLoadComplete(List<PubListDetails> data);
}

让你的片段类实现这个接口:

Let your fragment class implement this interface:

public class LocalFragment extends SherlockListFragment implements OnLoadDataListener {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {    
        super.onActivityCreated(savedInstanceState);
        LoadDataFromURL url_data = new LoadDataFromURL();
        url_data.setContext(getSherlockActivity());
        url_data.setURL("http://url.com/get_data/");
        url_data.setListener(this); // you could unite the setContext() method in the listener registration
        url_data.execute();
    }

    @Override
    public void onLoadComplete(List<PubListDetails> data) {
         ListAdapter adapter = new PubListAdapter(getSherlockActivity(), pubs);
         our_context.setListAdapter(adapter);
    }
}

而在你的的AsyncTask

private OnLoadDataListener mListener

public void setListener(OnLoadDataListener listener){
    mListener = listener;
}

onPostExecute 将数据发送到监听器:

and in the onPostExecute you send the data to the listener:

@Override
protected void onPostExecute(String result) {
    Toast.makeText(our_context, "ff", Toast.LENGTH_SHORT).show();
    // TODO Auto-generated method stub
    pDialog.dismiss();
    if (mListener != null) {
        mListener.onLoadComplete(pubs);
    }
}

这篇关于外部AsyncTask的填充的ListView从片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆