I wrote code using AsyncTask, which, when finished in onPostExecute, should change the static variable of the main class. AsyncTask starts at the touch of a button, and changes the variable only after it has been fully processed, and not during the time. As a result, you have to press twice to change the value of a variable. How can this be fixed?
The method in which the variable should change (is in a different class):
@Override // protected void onPostExecute(String s) { super.onPostExecute(s); String resultPOST = "Сервер ничего не выдал"; resultPOST = s; MainActivity.serverAnswer+=resultPOST; } Main class:
public class MainActivity extends AppCompatActivity { static Button logInButton; static String serverAnswer = "Ответ от сервера:\n"; String pass,login; public static TextView serverAnswerTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); logInButton = (Button) findViewById(R.id.button); serverAnswerTextView =(TextView) findViewById(R.id.textView); } public void onClickLogIn(View v){ TextView mLogIn = (TextView) findViewById(R.id.editText); login = mLogIn.getText().toString(); TextView mPass = (TextView) findViewById(R.id.editText2); pass = mPass.getText().toString(); new LocationSender().execute(login); serverAnswerTextView.setText(serverAnswer); } Forgot to add. If in onPostExecute I change the value of the TextView directly (not through a variable), it changes immediately, without pressing again.
@Override protected void onPostExecute(String s) { String resultPOST = "Сервер ничего не выдал"; resultPOST = s; MainActivity.serverAnswer+=resultPOST; //текст метки меняется сразу же MainActivity.serverAnswerTextView.setText (MainActivity.serverAnswerTextView.getText()+resultPOST); }
AsyncTaskworks. I recommend to watch video tutorials on asyncaster before trying to use them - Vladyslav Matviienko