The first factor to note in this situation is that calling the setOnItemSelectedListener()
method enforces the implementation of the AdapterView.onItemSelectedListener
interface in the class. An example:
public class JSONOAsyncTask
extends AsyncTask<String, Void, Boolean>
implements AdapterView.OnItemSelectedListener{
...
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Another factor is that you should explore AsyncTask
. / a>. This class gives us some interesting features : onPreExecute()
and onPostExecute()
. These methods are used to define some action that we want to perform before the asynchronous task and what we want to do after the task is completed.
We go to this
First you should know, like every programming language, that there are some reserved words. In JAVA, it has this and super , respectively the instructions this()
and super()
.
Key words are divided into categories according to their purpose. Some words are used in more than one scenario. The meaning of the word depends on the context in which it is used. This is called: semantic loading. Java, having a few key words and purposely re-using words whenever possible has a much greater semantic charge than other languages. However, in practice it is not possible to confuse the various meanings.
The keyword super is used to refer to super class methods or attributes. Imagine that you have a class named Manager that inherits from the Employee class and that both classes have a method named calcularPontos()
. In the Manager class how do I call the calcularPontos()
method of the superclass (Official)?
//o compilador sabe que você quer utilizar o método da superclasse e não o método local.
super.calcularPontos();
Reserved keyword is typically used within methods that receive parameters with the same class instance attribute name or to refer to the object itself , let's look at an example:
public void setNome(String nome){
/*o this nesse caso informar que o atributo de instancia
"nome" vai receber o valor do paramentro "nome".
Se não tivesse o this, como ele saberia? ficaria ambiguo.*/
this.nome = nome;
}
Another example, imagine that you are in the Gerente
class and call the method of another class that expects as an argument an object of class Gerente
. Then you can use this
to do this.
/* o método salvar gerente espera como argumento um objeto da classe
Gerente, como estou dentro da classe gerente eu disse que o
objeto a ser salvo é "este"(this).*/
Armazenamento.salvarGerente(this);
this: used to indicate that the intended scope for the invocation of
a method or access to an attribute is that of the current object itself.
Also used to refer to another builder of the same
class. It is still using as a way of referring to the instance that encapsulates
the current instance when in a nested class
A comparison
super: used to indicate that the intended scope for the invocation of
a method or access to an attribute is that of the parent class. Also used
to refer to another class constructor immediately
upper in inheritance hierarchy
Situation
See a way you could solve this case. First implementing AdapterView.OnItemSelectedListener
to superclass JSONAsyncTask
. Then declaring Spinner
as the global variable. In onPreExecute
the connection between XML and JAVA is made, which would be before executing doInBackGround
. In background the category list is created as an example, as well as the adapter definition. Finally, in% w / o%, the data is appended to the adapter. Here is exactly how your% custom% should be:
public class JSONOAsyncTask extends AsyncTask<String, Void, Boolean> implements AdapterView.OnItemSelectedListener{
private Spinner spinner;
private ArrayAdapter<String> dataAdapter;
@Override
protected void onPreExecute() {
super.onPreExecute();
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}
@Override
protected Boolean doInBackground(String... urls) {
// criando lista de elementos
List<String> categories = new ArrayList<String>();
categories.add("Balaco Bacco");
categories.add("Capitão G. Nascimento");
categories.add("JBuenos's Dias");
categories.add("Marceleza");
categories.add("Ramarelo");
// criação do adapter
dataAdapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return false;
}
protected void onPostExecute(Boolean result) {
// anexando o adapter aos dados
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Recently I've asked a question that is What is the difference between methods to get a context? , where I comment that we can in addition to onPostExecute
, get the context in various ways, with different methods like AsyncTask
and this
that apparently has the same purpose. Read the answers and see more details on how to use them.
Reference