2015-04-03 35 views
1

我有一个片段活动。当我点击这个片段时,它激发了一个侦听器,它触发了一个AsyncTask。在移动到侦听器中的下一行代码之前,我需要异步任务结果,即我需要使asyncTask同步。对话框没有显示哪个导致问题,因为我需要等待asynTask返回之前继续

为此,我通常使用对话框来有效地让用户等待asyncTask onPostExecute()。但我的对话框并没有出现,我的代码正在通过asyncTask进入捆绑代码,然后添加空变量,悲伤的脸。

这是我的片段类的骨骼,让我知道如果你需要其他东西,我有意识张贴太多,但我相信它连接到我的课堂结构和事实我使用片段。

public class Login_StaggeredGrid_Fragment_Activity extends FragmentActivity 
{ 
    private ArrayList<String[]> gameSummaryTilesData; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     final FragmentManager fm = getSupportFragmentManager(); 

     // Create the list fragment and add it as our sole content. 
     if (fm.findFragmentById(android.R.id.content) == null) { 
      final StaggeredGridFragment fragment = new StaggeredGridFragment(); 
      fm.beginTransaction().add(android.R.id.content, fragment).commit(); 
     } 
    } 

    private class StaggeredGridFragment extends Fragment implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener 
    { 
       private StaggeredGridView mGridView; 
       private boolean mHasRequestedMore; 
       private TilesAdapter mAdapter; 

       @Override 
       public void onCreate(final Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setRetainInstance(true); 
       } 

       @Override 
       public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { 
        return inflater.inflate(R.layout.activity_sgv, container, false); 
       } 

       @Override 
       public void onActivityCreated(final Bundle savedInstanceState) { 
        List<NameValuePair> params = new ArrayList<NameValuePair>(); 

        //Encapsulate all within a post cereate from a async task or call a blocking http call 
        super.onActivityCreated(savedInstanceState); 

        mGridView = (StaggeredGridView) getView().findViewById(R.id.grid_view); 

        if (savedInstanceState == null) { 
         final LayoutInflater layoutInflater = getActivity().getLayoutInflater(); 
         View header = layoutInflater.inflate(R.layout.list_item_header_footer, null); 
         mGridView.addHeaderView(header); 
        } 

        if (mAdapter == null) { 
         mAdapter = new TilesAdapter(getActivity(), R.id.summary1_value); 
        } 

        for (String[] data : loginTilesData) { 
         mAdapter.add(data); //Add each loginTilesData TileAdapter element to an mAdapter where it will be further broken down and used by the TileAdapter 
        } 

        mGridView.setAdapter(mAdapter); 
        mGridView.setOnScrollListener(this); 
        mGridView.setOnItemClickListener(this); 
       } 

       @Override 
       public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) 
       { 

        try 
        { 
         // Loading Games in Background Thread 
         new GetGamesSummaryTiles().execute(); 
        } 
        catch (Exception e) 
        { 
         e.printStackTrace(); 
        } 
        Intent i = new Intent(Login_StaggeredGrid_Fragment_Activity.this, GamesSummary_Fragment_Activity.class); 
        i.putExtra("gamesSummaryTilesData", gameSummaryTilesData); 
        startActivity(i); 
       } 
      } 

     /** 
    * Background Async Task to get data for next activity by making HTTP Request 
    * */ 
    // Progress Dialog 
    private ProgressDialog qDialog; 
    // JSON parser class 
    JSONParser jParser = new JSONParser(); 
    String url_login ="http://XX.XX.XXX.XX/XXXX.php"; 
    class GetGamesSummaryTiles extends AsyncTask<String, String, String> 
    { 
     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() 
     { 
      super.onPreExecute(); 
      qDialog = new ProgressDialog(getBaseContext()); 
      qDialog.setMessage("Please wait..."); 
      qDialog.setIndeterminate(false); 
      qDialog.setCancelable(false); 
      qDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... args) 
     { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      // getting JSON string from URL 
      JSONObject jsonLogin = jParser.makeHttpRequest(url_login, "GET", params); 

      pk_http pk_dbComms = new pk_http(); 
      try { 
       gameSummaryTilesData = pk_dbComms.formatHttpResponse_SummaryTile(jsonLogin); 
      } catch (JSONException e) { 
       String test = e.getStackTrace().toString(); 
       e.printStackTrace(); 
      } 

      return jsonLogin.toString(); 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String jsonString) 
     { 
      // dismiss the dialog after getting all questions 
      qDialog.dismiss(); 
      // updating UI from Background Thread 
      /*runOnUiThread(new Runnable() { 
      public void run() { 

       } 
      });*/ 
     } 
    } 
} 
+0

将包代码放入** onPostExecute()**方法中。 – Wizard 2015-04-03 12:22:33

回答

2

把你的意图调用代码在你onPostExecute方法和您的问题将得到解决

移动下面的代码从onItemClick()来onPostExceute

Intent i = new Intent(Login_StaggeredGrid_Fragment_Activity.this, GamesSummary_Fragment_Activity.class); 
       i.putExtra("gamesSummaryTilesData", gameSummaryTilesData); 
       startActivity(i); 
+0

thx - 应该这样做。如果我想将这个asynctask移动到它自己的类中,并将其用于我所有的片段,该怎么办?我会通过什么? – Fearghal 2015-04-03 12:26:04

+0

创建一个Async类的基类,并将其称为要执行的任何片段和活动。 – Pankaj 2015-04-03 12:27:53

+0

如果我不能等待异步类,那意味着我必须在onPostExecute中完成所有工作,这可能还需要设置活动控制attr。我如何在单独的AsyncTask类中执行此操作 - 传递上下文? – Fearghal 2015-04-03 12:58:33

0

把你下面的代码在postExecute ()方法的异步任务...

Intent i = new Intent(Login_StaggeredGrid_Fragment_Activity.this, GamesSummary_Fragment_Activity.class); 
       i.putExtra("gamesSummaryTilesData", gameSummaryTilesData); 
       startActivity(i);