我有片段活动。当我点击片段时,它会触发一个触发AsyncTask的监听器。在转移到侦听器中的下一行代码之前,我需要Async任务结果,即我需要asyncTask是同步的。
为此,我通常使用一个对话框来有效地让用户等待asyncTask onPostExecute()。但是我的对话框没有出现,我的代码正在经过asyncTask并进入捆绑代码,然后添加空变量,悲伤的面孔。
这是我的片段类的骨头,让我知道你是否需要其他任何东西,我意识到发布太多但是我确信它与我的类的结构有关,而且事实上我正在使用片段。 / p>
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 :(得分:2)
将您的意图调用代码放入onPostExecute方法中,您的问题将得到解决
将以下代码从onItemClick()移至onPostExceute
Intent i = new Intent(Login_StaggeredGrid_Fragment_Activity.this, GamesSummary_Fragment_Activity.class);
i.putExtra("gamesSummaryTilesData", gameSummaryTilesData);
startActivity(i);
答案 1 :(得分:0)
将您的以下代码放在asyncTask的postExecute()方法中......
Intent i = new Intent(Login_StaggeredGrid_Fragment_Activity.this, GamesSummary_Fragment_Activity.class);
i.putExtra("gamesSummaryTilesData", gameSummaryTilesData);
startActivity(i);