2014-03-01 104 views
1

我試圖找到一種方法來獲取元素的最後部分並將其設置爲textView 這裏是我的代碼獲取元素並將其設置爲TextView中。如何使用Jsoup在Android中獲取元素的一部分

Element burnabyStatus = doc.getElementsByClass("main-campus-status").first(); 
String b_status = burnabyStatus.text(); 
TextView tv_b_status = (TextView) findViewById(R.id.b_status); 
tv_b_status.setTypeface(tf, Typeface.BOLD); 
tv_b_status.setText(b_status); 

由於現在burnabyStatus.text()是等於「伯納比校園開放」,但我想我的TextView只顯示「打開」或「關閉」了,當它在網站上的變化。 這裏是我從哪裏得到的信息從我的網站here

回答

2

看完網站後,你需要「打開」的字是在「main-campus-status」div元素下的「h1」子元素,因此你需要做的就是讓「H1」元素的文本,你可以做這樣的:

Element burnabyStatus = doc.getElementsByClass("main-campus-status").first(); 
Elements h1 = burnabyStatus.select("h1"); 
burnabyStatus = h1.get(0); 
String b_status = burnabyStatus.text(); 
TextView tv_b_status = (TextView) findViewById(R.id.b_status); 
tv_b_status.setTypeface(tf, Typeface.BOLD); 
tv_b_status.setText(b_status); 

我添加的行是:

Elements h1 = burnabyStatus.select("h1"); 
burnabyStatus = h1.get(0); 

它選擇當y時,所有h1元素在「主校園狀態」div下ou調用「text()」只會讓你「打開」或「關閉」。

相關問題