简体   繁体   English

异步任务在FragmentActivity中不起作用

[英]Async task is not working in FragmentActivity

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 ie it is not getting the html from the website the same coding is working with activity but in fragmentactivity it is not? 我在FragmentActivity中有一个异步任务,我正在使用jsoup获取表的html,到目前为止,我发现doInBackground()无法正常工作,即它没有从网站获取html,而相同的编码正在处理活动但是在fragactivity中不是吗?

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. 该应用程序正在工作,但我无法在ListView中看到来自站点的数据。
Please point me out what is missing? 请指出我所缺少的是什么?

Move new JsoupListView().execute(); 移动new JsoupListView().execute(); to onResume() instead of onCreate() . onResume()而不是onCreate() At onCreate the mProgressDialog wont show. onCreate ,不会显示mProgressDialog Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM