Hi, I have this piece of code that gives a link and goes to the net with jsoup takes some information and then puts that information into an ArrayList used in a ListView. But the problem is that I do not know why the Array course is not full, the site changes depending on the letter in the url for this and that I make this code. I do not understand why and it does not fill the variable courses of .lin-area-c2 that are on the site. Here is the code:
public class Chose_curso extends AppCompatActivity {
private TextView pickLetraText;
private ListView cursoList;
private Spinner letraPicker;
private ArrayList<String> letras = new ArrayList<String>();
private ArrayList<String> cursos = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chose_curso);
pickLetraText = (TextView) findViewById(R.id.pickLetraText);
cursoList = (ListView) findViewById(R.id.cursoList);
letraPicker = (Spinner) findViewById(R.id.letraPicker);
letras.add("A");
letras.add("B");
letras.add("C");
letras.add("D");
letras.add("E");
letras.add("F");
letras.add("G");
letras.add("H");
letras.add("I");
letras.add("J");
letras.add("L");
letras.add("M");
letras.add("N");
letras.add("O");
letras.add("P");
letras.add("Q");
letras.add("R");
letras.add("S");
letras.add("T");
letras.add("U");
letras.add("V");
ArrayAdapter<String> adapterLetras = new ArrayAdapter<String>(Chose_curso.this,android.R.layout.simple_spinner_item,letras);
adapterLetras.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
letraPicker.setAdapter(
new NothingSelectedSpinnerAdapter(
adapterLetras,
R.layout.contact_spinner_row_nothing_selected,
// R.layout.contact_spinner_nothing_selected_dropdown, // Optional
this));
Document document = null;
letraPicker.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setCursoList();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});}
public void setCursoList(){
try {
if(letraPicker.getSelectedItem()!=null){
cursos.clear();
Document document = null;
String site ="http://www.dges.gov.pt/guias/indcurso.asp?letra="+(letraPicker.getSelectedItem().toString());
document = Jsoup.connect(site).get();
for(int contador=0;contador<document.select(".lin-area-c2").size();contador++){
cursos.add(String.valueOf((document.select(".lin-area-c2").get(contador).text())));}}
} catch (Exception e) {
e.printStackTrace();
}
ArrayAdapter<String> cursosAdapter = new ArrayAdapter<String>(Chose_curso.this,android.R.layout.simple_spinner_item,cursos);
cursoList.setAdapter(cursosAdapter);
}
}
Website link link in this case with the letter B selected. What I'm doing and basically give the user the opportunity to choose which letter and what they want in the url when I go get the information!
To resolve I did this in the way I indicated:
public List<String> buscarCursos(final String letra) throws IOException {
final List<String> cursos1 = new ArrayList<>();
final ArrayAdapter<String> cursosAdapter = new ArrayAdapter<String>(Chose_curso.this,android.R.layout.simple_spinner_item,cursos1);
cursoList.setAdapter(cursosAdapter);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
String site = "http://www.dges.gov.pt/guias/indcurso.asp?letra=";
Document document;
Elements lista;
document = Jsoup.connect(site +letra).get();
lista = document.select(".lin-area-c2");
for (Element elemento: lista) {
cursos1.add(elemento.text());
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
return cursos1;
}
I used the search courses to remove all courses for each letter as the answer I was given.