簡體   English   中英

如何在onclicklistner上調用asynctask?

[英]how to call asynctask on onclicklistner?

**如何在onclicklistner按鈕上調用Asynktask **

 b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

        uname = e1.getText().toString();
        upwd = e2.getText().toString();
        ubranch=s1.getSelectedItem().toString();
        if(uname.equals("")||upwd.equals(""))
            {
                Toast.makeText(getApplication(), "u cant leave this      
      empty", Toast.LENGTH_LONG).show();
            }
        else
        {
            //i want to call test class here 
        }
        }

    });

**並且我一直在尋找這個,但是找不到完美的例子**

 ** here is the method that i have used for login** 

如我所知,這個Asyntask將是一個工作線程,所以Toast可以與此線程一起工作..而我不工作,請告訴我為什么它不與工作線程一起工作

 public boolean check() {
    ArrayList<NameValuePair> nameValuePairs = new    
   ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("uname", uname));
    nameValuePairs.add(new BasicNameValuePair("upwd", upwd));
    nameValuePairs.add(new 
    BasicNameValuePair("uroll",s1.getSelectedItem().toString()));

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("phpfile");
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("login pass1", "connection sucess");

    } catch (Exception e) {
        Log.e("login failed1", e.toString());
        Toast.makeText(getBaseContext(), "Connect to the Internet",
                Toast.LENGTH_SHORT).show();
    }
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        Log.e("login pass 2", "Connection successfull");

    } catch (Exception e) {
        Log.e("login failed2", e.toString());
    }

    try {
        JSONObject object = new JSONObject(result);
        JSONArray array = object.getJSONArray("res");

        if (array.length() > 0)
        {
            for (int i = 0; i < array.length(); i++) {

                JSONObject jsonObject = array.getJSONObject(i);
                uname = jsonObject.getString("uname");
                upwd = jsonObject.getString("upwd");
                uroll=jsonObject.getString("uroll");
                String id1=jsonObject.getString("id");

        SharedPreferences sd = 
         getSharedPreferences("mypref3",MODE_PRIVATE);
                SharedPreferences.Editor edt = sd.edit();
                edt.putString("unamee", uname);
                edt.putString("uroll", uroll);
                edt.putString("id", id1);
                edt.commit();

                if(uroll.equals("Employee"))
                {
                Intent i1 = new Intent(login.this, emp_dashboard.class);
                    startActivity(i1);
                }
                if(uroll.equals("Manager"))
                {
                    Intent i1 = new   
   Intent(login.this,managerdashboard.class);
                    startActivity(i1);
                 }

            }
             Toast.makeText(getBaseContext(), "login  successfull ", 
    Toast.LENGTH_LONG).show();

            e1.setText("");
            e2.setText("");
        }



    } catch (Exception e) {
        Log.e("login failed3", e.toString());
            Toast.makeText(getBaseContext(), "invalid user name or     
  password", Toast.LENGTH_LONG).show();
      }
    return false;
}





 ** here is test class which extends Asynktask **
** i hope i have implemented it correctly **


 private class test extends AsyncTask<Void, Void, Void>{

       ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
       progressDialog = ProgressDialog.show(login.this, "", 
"loading...", true, false);
    }

    @Override
    protected Void doInBackground(Void... params) {
        check(); // method that handles the response
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
    }
}

閱讀有關AsyncTasks的指南非常有幫助,因為它們非常全面。 話雖如此。 您可以做的最簡單的事情是創建一個擴展AsyncTask的非常簡單的自定義類,您只需在自定義AsyncTask的doInBackground調用check doInBackground 在不為您編寫所有代碼的情況下,您可以在AsyncTask中擁有一個使用用戶名和密碼的構造函數,並在您的點擊監聽器中將其調用為:

b1.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) {

    uname = e1.getText().toString(); 
    upwd = e2.getText().toString(); 
    ubranch=s1.getSelectedItem().toString(); 
    if(uname.equals("")||upwd.equals("")) 
        { 
            Toast.makeText(getApplication(), "u cant leave this       
  empty", Toast.LENGTH_LONG).show(); 
        } 
    else 
    { 
        new LoginAsyncTask(uname, upwd).execute();
    } 
    } 

}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM