我怎么能做吐司消失?

时间:2013-04-04 02:41:21

标签: android android-asynctask toast

我有吐司的问题。当用户成功登录数据库时,如何使吐司消失。

用户的代码无法像这样登录:

类BuatLogin扩展了AsyncTask {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Login_layout.this);
        pDialog.setMessage("Login_layout Progress...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    protected String doInBackground(String... args) {
        String usr = user.getText().toString();
        String pwd = pass.getText().toString();

        Log.d("1 "+usr, pwd);
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("usr", usr));
        params.add(new BasicNameValuePair("pwd", pwd));
        Log.d("2 "+usr, pwd);
        Log.d(usr,url_create_login);

        // getting JSON Object
        JSONObject json = jsonParser.makeHttpRequest(url_create_login,
                "POST", params);
        Log.d("Buat Respond", json.toString());
        try {
            int sukses = json.getInt(TAG_SUKSES);

            if (sukses == 1) {
                String nim=json.getString(TAG_NIM);
                String jrs=json.getString(TAG_JRS);
                Log.d(TAG_NIM,nim);

                // sukses login
                Intent i = new Intent(getApplicationContext(), Mhs_main_layout.class);
                i.putExtra(TAG_NIM, nim);
                i.putExtra(TAG_JRS,jrs);
                startActivity(i);
                finish();
            } else if(sukses == 2) {
                String nip=json.getString(TAG_NIP);
                Log.d(TAG_NIP,nip);
                Intent i = new Intent(getApplicationContext(), Admin_main_layout.class);
                i.putExtra(TAG_NIP, nip);
                startActivity(i);

                finish();
            }else if(sukses == 3){
                setResult(100);
                //toas(100);
                //finish();

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


        return null;

    }

    //Respon dari upadte buku class
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        int resultCode = 100;
        if (resultCode != 100); 
        {
         Toast.makeText(Login_layout.this, "Nip/Nim Atau Password TIdak Sesuai Silahkan Coba Lagi ", Toast.LENGTH_LONG).show();
        }
    }

}

但是即使用户成功登录,toast仍然会出现,并且日志猫没有错误。我该怎么办才能让它消失?

2 个答案:

答案 0 :(得分:0)

好吧,如果调用setResult(100);将变量resultCode设置为100,则测试if (resultCode == 100)将始终为true,因此将始终显示toast。

您确定不想测试if (resultCode != 100)吗?

答案 1 :(得分:0)

以下是您需要以squeletal形式进行的操作:

int doInBackground(...) {
  blablabla...

  if (sukses == 1) {
     blablabla...
     // The value 1 will be returned to onPostExecute(int resultCode)
     // as the value for the parameter "resultCode":
     return 1; // or return sukses; same thing.
  } else if (sukses == 2) {
     blablabla...
     return 2; // or return sukses; same thing.
  } else {
     blablabla...
     return 3; // or return sukses; same thing.
  }
}

void onPostExecute(int resultCode) {
  // resultCode now contains the value returned by doInBackground():
  if (resultCode== 1) {
    Toast.makeText(Login_layout.this, "1", Toast.LENGTH_LONG).show();
    finish();
  } else if (resultCode== 2) {
    Toast.makeText(Login_layout.this, "2", Toast.LENGTH_LONG).show();
    finish();
  } else {
    Toast.makeText(Login_layout.this, "3", Toast.LENGTH_LONG).show();
  }
}

随意根据您的需要完成此骨架。我已将调用fin()转移到onPostExecute(),以确保在发布Toast之前线程不会被取消,但您应该搜索Google或进行自己的测试以找到最佳选择。< / p>