按钮需要点击两次异步任务完成加载



下面是我的LoginActivity的代码,其中我基本上从URl获得JSON和图像在AsyncTask中。奇怪的是,我的onClick()的else部分总是执行(i)。(祝酒词显示)。只有当我再次点击按钮时,它才跳转到下一个活动。

public class LoginActivity extends Activity
{
    Button getBunks;
    CheckBox detailscheck;
    EditText regText, bdayText;
    String storedReg, storedBDay;
    public static final String PREFS_NAME = "MyPrefsFile";
    String predataURL;
    String prephotoURL;
    public static URL dataUrl;
    public static String dataUri = "http://ankit.im/websisAPI/parser.php";
    ProgressDialog progressDialog;
    JSONObject temp = null;
    Bitmap tmpPhoto;
    boolean doneFlag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getBunks = (Button) findViewById(R.id.getBunkButton);
        detailscheck = (CheckBox) findViewById(R.id.rememberOpts);
        regText = (EditText) findViewById(R.id.regNo);
        bdayText = (EditText) findViewById(R.id.bday);
        detailscheck.setChecked(true); // To avoid Nag
        try
        {
            dataUrl = new URL("http://ankit.im/websisAPI/parser.php");
        } catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        // Check if login details are already stored
        SharedPreferences prefs = this.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        storedReg = prefs.getString("regno", null);
        storedBDay = prefs.getString("bday", null);
        // The Following Listeners Check if the user has entered
        // appropriate Data in the Fields
        // Check is on enter key press and on Focus Change
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        final SharedPreferences.Editor editor = settings.edit();
        regText.setOnFocusChangeListener(new OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {
                if (detailscheck.isChecked())
                {
                    if (!hasFocus)
                    {
                        if (regText.getText().toString() != "" || regText.getText() != null)
                        {
                            editor.putString("regno", regText.getText().toString());
                            editor.commit();
                            // Toast.makeText(getApplicationContext(),
                            // "Reg No Stored", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
        bdayText.setOnEditorActionListener(new OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
            {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || event.getAction() == KeyEvent.ACTION_DOWN
                        && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
                {
                    editor.putString("bday", bdayText.getText().toString());
                    editor.commit();
                    // Hide Virtual Keyboard
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(regText.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });
        bdayText.setOnFocusChangeListener(new OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {
                if (detailscheck.isChecked())
                {
                    if (!hasFocus)
                    {
                        if (bdayText.getText().toString() != "" || bdayText.getText().toString() != null)
                        {
                            editor.putString("bday", bdayText.getText().toString());
                            editor.commit();
                            // Toast.makeText(getApplicationContext(),
                            // "BDay Stored", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
        // Handle the button click
        getBunks.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                new DownloadStudentData().execute();
                if (doneFlag == true)
                {
                    // GlobalVars.showAttendanceData();
                    Intent actChange = new Intent(v.getContext(), DetailsTabViewActivity.class);
                    startActivity(actChange);
                    finish();
                } else if (doneFlag == false)
                {
                    Toast.makeText(getApplicationContext(), "Error in Downloading, Please Check Details or Try Again", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    protected void onStart()
    {
        super.onStart();
        if (storedReg != null && storedBDay != null)
        {
            regText.setText(storedReg);
            bdayText.setText(storedBDay);
        }
    }
    private class DownloadStudentData extends AsyncTask<Void, Void, Void>
    {
        // ProgressDialog progressDialog;
        final JSONParser jsp = new JSONParser();
        @Override
        protected void onPreExecute()
        {
            // super.onPreExecute();
            progressDialog = new ProgressDialog(LoginActivity.this);
            progressDialog.setCancelable(true);
            progressDialog.setTitle(" Downloading Data ");
            progressDialog.setMessage("Please Wait, Loading...");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            // progressDialog.setProgress(0);
            progressDialog.show();
        }
        @Override
        protected Void doInBackground(Void... voids)
        {
            temp = new JSONObject();
            temp = jsp.getJSONFromUrl(dataUri); // get the data
            prephotoURL ="http://218.248.47.9/websis/control/img/P12703.JPGimgId=P12703";
            try
            {
                InputStream in = new java.net.URL(prephotoURL).openStream();
                tmpPhoto = BitmapFactory.decodeStream(in);
            } catch (Exception e)
            {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void v)
        {
            GlobalVars.setJSON(temp);
            GlobalVars.setPhotoURL(prephotoURL);
            GlobalVars.setBitmap(tmpPhoto);
            doneFlag = true;
            try
            {
                GlobalVars.setPhotoURL(temp.getString("photolink"));
            } catch (JSONException e)
            {
                e.printStackTrace();
            }
            progressDialog.dismiss();
        }
    }
    @Override
    protected void onPause()
    {
        super.onPause();
        if (progressDialog != null)
            progressDialog.dismiss();
    }
}

GlobalVars只是一个包含公共静态变量和方法的全局类。

我也不确定这是否是从后台URL下载图像的正确方法,任何更好的替代方案都将受到赞赏(URLImageViewHelper不适合我的需要)。

AsyncTask在不同的线程上运行,并且不会及时完成以更改doneFlag的值。(这就是为什么它是异步的。)

new DownloadStudentData().execute(); // Takes time to complete
if (doneFlag == true) { ... } // Happens milliseconds later, AsyncTask still running
else if (doneFlag == false) { ... } 

您需要在onPostExecute()中运行任何"完成"代码。


几个快速提示:

  • if (doneFlag)if (doneFlag == true)的缩写。
  • 一个布尔值只能有两种状态,你不需要使用else if(),只需要使用else {...}

最新更新