我正在與Jsoup和aSyncTask的應用程序工作。我想在列表視圖中獲取一個html表格,但我在列表視圖中獲取數據時遇到問題。我知道我應該使用onPostExecute方法。錯誤是該方法沒有被調用,所以你應該使用@Override所以方法將被調用,但然後我有問題調用列表視圖和列表視圖中設置列表。列表視圖列表與onPostExecute
如果有人有其他解決方案,也將不勝感激。
這是我的代碼:
public class Cluka2 extends AsyncTask<Void, Void, String> {
Document document = null;
public List<String> list = new ArrayList<>();
ListView listView = null;
Context context = null;
public Cluka2(ArrayList<String> list,Context mContext) {
this.list = list;
this.context= mContext;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
protected String doInBackground(Void... params) {
try {
document = Jsoup.connect("https://tennisnaarden.planmysport.com/portal/page/pmsportal30/TVNaarden/Toernooien/Clubtoernooi").get();
Elements elements = document.select("#pcnt1383_8158836_1383_4326089_4326089 td:first-child");
for (int i = 0; i < elements.size(); i++) {
list.add(elements.get(i).text());
}
} catch (IOException e) {
e.printStackTrace();
}
return list.toString();
}
@Override
protected void onPostExecute(String result) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
Toernooien.setAdapter(arrayAdapter);
}
}
這段代碼有應用程序運行時沒有錯誤,但它並不顯示具有數據列表。
這是我的活動類:
public class ClubkampioenschappenSingleenDubbel extends AppCompatActivity {
ArrayList<String> list = new ArrayList<>();
Context context;
Document doc = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clubkampioenschappen_singleen_dubbel);
this.setTitle("Clubkampioenschappen Single en Dubbel");
new Cluka2(list, context).execute();
ListView Toernooien = (ListView) findViewById(R.id.Toernooien);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_clubkampioenschappen_singleen_dubbel, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
'context'爲空。它在哪裏初始化? – Raghunandan
上下文context = null;我試圖用onPostExecute方法在列表視圖中顯示列表。你需要上下文。 –
你需要上下文。您可以在應用程序組件(如Activity(ActivityName.this))中獲取它。或者在'getActivtiy()'片段中。 – Raghunandan