There was such a wonderful code: Native work with the Android interface . Added asynchrony, it turned out:
void OnLoginLayout(){ setContentView(R.layout.login); Button auth = (Button) findViewById(R.id.LoginLayout_Auth); auth.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ EditText login = (EditText) findViewById(R.id.LoginLayout_Login); EditText pass = (EditText) findViewById(R.id.LoginLayout_Password); LoginAsync mt = new LoginAsync(login.getText().toString(), pass.getText().toString()); mt.execute(); //if(Login(login.getText().toString(), pass.getText().toString())!=0) // OnMainLayout(); //else{ // String err=GetLoginError(); // TextView error = (TextView) findViewById(R.id.LoginLayout_Error); // error.setText(err); //} } }); auth = (Button) findViewById(R.id.LoginLayout_Reg); auth.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ OnRegisterLayout(); } }); return; } class LoginAsync extends AsyncTask<Void, Void, Void>{ String login, pass; int login_result; public LoginAsync(String l, String p){ login=l; pass=p; } @Override protected void onPreExecute(){ super.onPreExecute(); TextView error = (TextView) findViewById(R.id.LoginLayout_Error); error.setText("Login..."); } @Override protected Void doInBackground(Void... params){ login_result=Login(login, pass); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); if(login_result!=0) OnMainLayout(); else{ String err=GetLoginError(); TextView error = (TextView) findViewById(R.id.LoginLayout_Error); error.setText(err); } } } Looks pretty over-complicated. How can I optimize this code?
For me, the optimal solution seems to be:
LoginAsync mt = new LoginAsync(); int result; mt.setPre(new Pre{ TextView error = (TextView) findViewById(R.id.LoginLayout_Error); error.setText("Login..."); }); mt.setAsync(new Async{ result=Login(login.getText().toString(), pass.getText().toString()); }); mt.setAsync(new Post{ if(result) OnMainLayout(); else{ String err=GetLoginError(); TextView error = (TextView) findViewById(R.id.LoginLayout_Error); error.setText(err); } }); mt.execute(); Tell me how to implement it.
The same question: how to call the login code in one copy?