Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8865451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:30:47+00:00 2026-06-14T16:30:47+00:00

I am developing an app based off fragments. A lot of the content of

  • 0

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();

    }

} 

My LoadDataFromURL class is as follows:

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);
    }});

    }   

}

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.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:30:49+00:00Added an answer on June 14, 2026 at 4:30 pm

    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.

    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);
        }
    }
    

    And in your AsyncTask:

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

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a LAN-based database application. It involves a central server app to
Hi Guys so i am developing an app in Codeignitor. content is updated based
I'm developing a Pylons app which is based on exisitng database, so I'm using
I am trying to create a metro app based on a Developing windows 8
I am developing one app which is based on the webservice and fetch the
I am developing an quiz based app and i wanted to know how i
I am developing a quiz based app. The quiz contains 1 question and 4
I have a winforms (VB 2008) based app that I'm developing and I want
I'm developing an intranet-based web app that integrates with Facebook via the Graph API.
I am developing a simple sip-based voip app for android. I used this sample

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.