在ViewPager中使用Fragment进行AsyncTask

时间:2013-06-28 22:22:20

标签: android android-asynctask android-viewpager

首先,我对android编程比较陌生。

我正在创建一个包含两个片段的ViewPager应用程序。其中一个片段从服务器请求数据并将结果返回到主FragmentActivity。我的问题是这个对服务器的请求可能需要一段时间,并且我一直在尝试使用AsyncTask显示ProgressDialog,同时用户等待检索数据。一旦我创建了后台线程来检索数据,我就成功地在onPostExecute()方法中执行了一些代码并设置了一些变量。但是,在后台线程实际结束之前,正在执行将信息发送回FragmentActivity的return语句。我似乎无法弄清楚主线程在后台线程上等待的方法。使用Asyctask的get()方法会导致出现ProgressDialog。我在这里看了很多帖子,但似乎找不到答案。

一切都有帮助。

以下代码:

SplashScreen.java

public class SplashScreen extends FragmentActivity {
    MainMenu mainMenu;
    MapScreen mapScreen;
    PagerAdapter pagerAdapter;
    ViewPager viewPager;
    List<LatLng> geoPoints;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

            setContentView(R.layout.activity_splash_screen);
            context = this;

            initializePaging();
    }

    private void initializePaging()
    {
            mainMenu = new MainMenu();
            mapScreen = new MapScreen();

            pagerAdapter = new PagerAdapter(getSupportFragmentManager());
            pagerAdapter.addFragment(mainMenu);
            pagerAdapter.addFragment(mapScreen);

            viewPager = (ViewPager) super.findViewById(R.id.viewPager);
            viewPager.setAdapter(pagerAdapter);
            viewPager.setOffscreenPageLimit(2);
            viewPager.setCurrentItem(0);

            viewPager.setOnPageChangeListener(new OnPageChangeListener()
            {
                    @Override
                    public void onPageScrollStateChanged(int postion){}
                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2){}
                    @Override
                    public void onPageSelected(int position)
                    {
                    switch(position){
                            case 0: findViewById(R.id.first_tab).setVisibility(View.VISIBLE);
                                    findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
                                    break;

                            case 1:        findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
                                    findViewById(R.id.second_tab).setVisibility(View.VISIBLE);
                                    break;
                            }
                    }
            });
}

    //Called from onClick in main_mainu.xml
    public void getDirections(View view)
    {
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

            try
            {
                    geoPoints = mainMenu.getDirections(context);
                    mapScreen.plotPoints(geoPoints);

            }
            catch(Exception e)
            {
                    Toast.makeText(getApplicationContext(), "Error! Invalid address entered.", Toast.LENGTH_LONG).show();
                    mainMenu.clear();
            }

    }

}

MainMenu.java

public class MainMenu extends Fragment {

    String testString;
    int testInt;
    TextView testTV;
    private TextView tvDisplay;
    private EditText departure;
    private EditText destination;
    private Geocoder geocoder;
    private List<Address> departAddress;
    private List<Address> destinationAddress;
    private List<LatLng> geoPoints;
    private String departString;
    private String destinationString;
    private Address departLocation;
    private Address destinationLocation;
    private LatLng departurePoint;
    private LatLng destinationPoint;
    private Context contextMain;
    private GetData task;

    public MainMenu()
    {
            super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
            View root = (View) inflater.inflate(R.layout.main_menu, null);

            geoPoints = new ArrayList<LatLng>(2);

            return root;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
            departure = (EditText) getView().findViewById(R.id.depart_field);
            destination = (EditText) getView().findViewById(R.id.destination_field);

            tvDisplay = (TextView) getView().findViewById(R.id.textView1);

    }

    public List<LatLng> getDirections(Context context)
    {       
            contextMain = context;
            geocoder = new Geocoder(getActivity());

            departString = departure.getText().toString();
            destinationString = destination.getText().toString();

            try
            {
                    task = new GetData(new Callback(){
                            public void run(Object result)
                            {
                                    //return geoPoints;
                            }
                    });
                    task.execute((Void[])null);
            }catch(Exception e)
            {
                    e.printStackTrace();
            }

            return geoPoints; 
    }

    public void clear()
    {
            departure.setText("");
            destination.setText("");
            tvDisplay.setText("Enter departure point, and destination point");
    }

    private class GetData extends AsyncTask<Void, Void, List<List<Address>>>
    {
            Callback callback;
            private ProgressDialog processing;

            public GetData(Callback callback)
            {
                    this.callback = callback;
            }

            @Override
            protected void onPreExecute()
            {
                    processing = new ProgressDialog(contextMain);
                    processing.setTitle("Processing...");
                    processing.setMessage("Please wait.");
                    processing.setCancelable(false);
                    processing.setIndeterminate(true);
                    processing.show();

            }

            @Override
            protected List<List<Address>> doInBackground(Void...arg0)
            {       
                    List<List<Address>> list = new ArrayList<List<Address>>(2);

                    try
                    {
                            departAddress = geocoder.getFromLocationName(departString, 5, 37.357059, -123.035889, 38.414862, -121.723022);
                            destinationAddress = geocoder.getFromLocationName(destinationString, 5, 37.357059, -123.035889, 38.414862, -121.723022);

                            list.add(departAddress);
                            list.add(destinationAddress);

                    }catch(IOException e)
                    {
                            e.printStackTrace();
                    }

                    return list;
            }

            @Override
            protected void onPostExecute(List<List<Address>> list)
            {
                    departLocation = list.get(0).get(0);
                    destinationLocation = list.get(1).get(0);

                    departurePoint = new LatLng(departLocation.getLatitude(), departLocation.getLongitude());
                    destinationPoint = new LatLng(destinationLocation.getLatitude(), destinationLocation.getLongitude());

                    if(geoPoints.size() >= 2)
                    {
                            geoPoints.clear();
                    }

                    geoPoints.add(departurePoint);
                    geoPoints.add(destinationPoint);

                    callback.run(list);
                    processing.dismiss();
            }
    }

}

1 个答案:

答案 0 :(得分:1)

        @Override
        protected Object doInBackground(Void...arg0)
        {
                Object result = null;

                try
                {
                        departAddress = geocoder.getFromLocationName(departString, 5, 37.357059, -123.035889, 38.414862, -121.723022);
                        destinationAddress = geocoder.getFromLocationName(destinationString, 5, 37.357059, -123.035889, 38.414862, -121.723022);

                }catch(IOException e)
                {
                        e.printStackTrace();
                }

                return result;
}

您永远不会设置结果的值......