0

I've a Async task in a FragmentActivity i'm using jsoup to get the html for table what i found out so far is doInBackground() is not working i.e it is not getting the html from the website the same coding is working with activity but in fragmentactivity it is not?

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new JsoupListView().execute();

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });


}



@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
// Title AsyncTask
private class JsoupListView extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(MainActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Android Jsoup ListView Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }


    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();

        try {
            // Connect to the Website URL
            Document doc = Jsoup.connect(url).get();


            for (Element table : doc.select("table[id=Table3]")) {

                // Identify all the table row's(tr)
                for (Element row : table.select("tr:gt(0)")) {
                    HashMap<String, String> map = new HashMap<String, String>();

                    // Identify all the table cell's(td)
                    Elements tds = row.select("td");

                    // Identify all img src's
                    Elements imgSrc = row.select("img[src]");
                    // Get only src from img src
                    String imgSrcStr = imgSrc.attr("src");

                    // Retrive Jsoup Elements
                    // Get the first td
                    map.put("field0", tds.get(0).text());
                    //System.out.println(("check"+tds.get(0).text()));
                    // Get the second td
                    map.put("field1", tds.get(1).text());
                    // Get the third td
                    map.put("field2", tds.get(2).text());
                    // Get the image src links
                    map.put("flag", imgSrcStr);
                    // Set all extracted Jsoup Elements into the array
                    arraylist.add(map);
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    } 
    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(MainActivity.this, arraylist);
        // Set the adapter to the ListView
         listview.setAdapter(adapter);


        //listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}

The application is working but i can not see data from the site in the ListView.
Please point me out what is missing?

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
Osama Aftab
  • 1,161
  • 9
  • 15

1 Answers1

0

Move new JsoupListView().execute(); to onResume() instead of onCreate(). At onCreate the mProgressDialog wont show. Hope this helps.

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71