我正在使用 Jsoup 和 aSyncTask 开发一个应用程序。我试图在 ListView 中获取 html 表,但在获取 ListView 中的数据时遇到问题。我知道我应该使用 onPostExecute 方法。错误是该方法没有被调用,所以你应该使用@Override,这样方法就会被调用,但是我在调用 ListView 和在 ListView 中设置列表时遇到问题。
如果有人有替代解决方案,我们也将不胜感激。
这是我的代码:
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);
}
}
当应用程序运行时,此代码没有错误,但它没有显示包含数据的列表。
这是我的 Activity 类(class):
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);
}
}
最佳答案
您已经在 doInBackground()
中设置了列表,因此,您可以将结果字符串作为“成功”或“失败”传递。并在此基础上,您可以在 onPostExecute()
根据您的 AsynsTask 结构,您的 onPostExecute()
方法应如下所示:
@override
protected void onPostExecute(String result) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
Onderdelen.setAdapter(arrayAdapter);
}
除此之外,您还必须在构造函数中启动上下文。
public Cluka2(ArrayList<String> list,Context mContext) {
this.list = list;
this.context=mContext;
}
此外,您还需要在 Activity 的 onCreate()
上启动 Listview
。因此,从 onPostExecute()
中删除这一行并将其放入 onCreate()
中。
ListView Onderdelen = (ListView) listView.findViewById(R.id.Toernooien);
编辑:
在您的 Activity 的 onCreate() 中:
改变
new Cluka2(list, context).execute();
ListView Toernooien = (ListView) findViewById(R.id.Toernooien);
至
ListView Toernooien = (ListView) findViewById(R.id.Toernooien);
new Cluka2(list, ClubkampioenschappenSingleenDubbel.this).execute();
关于java - 使用 onPostExecute 在 ListView 中列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823393/