【发布时间】:2020-02-15 19:29:41
【问题描述】:
所以我正在使用 jsoup 库抓取一些数据。数据组织在 html 元素表中。我想在 textView 或 listView 中显示相关数据。对于初学者,它需要在 textView 中。当我尝试显示来自多个表的信息时,textView 只显示第一个表。我不能把我的想法包裹在这件事上。希望你能帮助我指出我做错了什么。
这里是代码
TextView textView;
Button dohvatiStranicu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
textView.setMovementMethod(new ScrollingMovementMethod());
dohvatiStranicu = (Button)findViewById(R.id.getPageButton);
dohvatiStranicu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new dohvatiStranicu().execute();
}
});
}
public class dohvatiStranicu extends AsyncTask<Void,Void,Void>{
StringBuilder stringBuilder;
@Override
protected void onPreExecute() {
stringBuilder = new StringBuilder();
}
@Override
protected Void doInBackground(Void... voids) {
try{
Document doc = Jsoup.connect("https://inf.ffzg.unizg.hr/index.php/hr/studij/diplomski-studij/ispitni-rokovi?fbclid=IwAR0WuLXdooI_0wB8-vVbgZTs89jX-B0eNY0f4wmB9rScqojSqsA2oN-aQ6I").get();
Elements tables = doc.select("table");
for(Element table : tables){
stringBuilder.append("\n\n\n");
stringBuilder.append(parsirajTablicu(table));
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
textView.setText(stringBuilder);
}
}
private static String parsirajTablicu(Element table){
String text = "\n\n\n\n\n";
Element nazivPredmeta = table.selectFirst("p");
Elements naziviRokova = table.select("th");
Elements datumiRokova = table.select("td");
datumiRokova.remove(0);
text += nazivPredmeta.text()+ "\n\n";
text += naziviRokova.get(0).text() + "\n";
text += " " + datumiRokova.get(0).text() + "\n";
text += " " + datumiRokova.get(4).text() + "\n";
text += " " + datumiRokova.get(8).text() + "\n";
text += naziviRokova.get(1).text() + "\n";
text += " " + datumiRokova.get(1).text() + "\n";
text += " " + datumiRokova.get(5).text() + "\n";
text += " " + datumiRokova.get(9).text() + "\n";
text += naziviRokova.get(2).text() + "\n";
text += " " + datumiRokova.get(2).text() + "\n";
text += " " + datumiRokova.get(6).text() + "\n";
text += " " + datumiRokova.get(10).text() + "\n";
text += naziviRokova.get(3).text() + "\n";
text += " " + datumiRokova.get(3).text() + "\n";
text += " " + datumiRokova.get(7).text() + "\n";
text += " " + datumiRokova.get(11).text() + "\n";
return text;
}
}
【问题讨论】: