Android - 使用 JSOUP 实现带有 AsyncTask 的 ListView
Android - ListView with AsyncTask implementation using JSOUP
我需要一些建议,因为这件事花了我足够多的时间让我因为缺乏知识而生自己的气...我尝试制作一个由 JSOUP-extracted 数据填充的 ListView。而 JSOUP 部分在 AsyncTask 中。这是我的代码:
public class ListaActivity extends ActionBarActivity {
private List<String> mList = new ArrayList<>();
private ListView mListView;
private ListAdapter mAdapter;
public Elements job;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mListView = (ListView) findViewById(R.id.list);
new NewThread().execute();
mAdapter = new ListAdapter(this, mList);
mListView.setAdapter(mAdapter);
}
public class NewThread extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg) {
Document doc;
try {
doc = (Document) Jsoup.connect("http://www.somewebsite.com")
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get();
title = doc.select("h3.something-to-extract a[href]");
for (Element titles : title) {
mList.add(titles.text()+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
IMO 它与 JSOUP 部分有关,因为当我擦除所有内容 od doInBackground
并只放入
mList.add("Something 1");
mList.add("Something 2");
然后就可以了。请以某种方式帮助我。
编辑:我想解析来自这个 html 片段的数据:
<h2 class="title">
<a href="/jstpl/london/11697582"
title="You just have to wait" class="titles">
Nothing else to say
</a>
我想将“Nothing else to say
”存储到 mList
,例如我的 html 代码中存在的另一个标题。单独的解析部分也很好用。
You have to call notifyDataSetChanged
on adapter
反映提供给适配器的列表的变化。为此 -
覆盖 NewThread
中的 onPostExecute
并调用 mAdapter.notifyDataSetChanged()
@Override
protected void onPostExecute(String result) {
mAdapter.notifyDataSetChanged();
}
注意: onPostExecute
在主 UI 线程上运行,而不是您的 NewThread
,其中 doInBackground
在内部运行你的NewThread
。 onPostExecute
在后台线程完成处理时调用。现在,因为我们已经用新项目更新了列表。我们将在主线程上通知适配器 运行。希望对你有帮助。
我需要一些建议,因为这件事花了我足够多的时间让我因为缺乏知识而生自己的气...我尝试制作一个由 JSOUP-extracted 数据填充的 ListView。而 JSOUP 部分在 AsyncTask 中。这是我的代码:
public class ListaActivity extends ActionBarActivity {
private List<String> mList = new ArrayList<>();
private ListView mListView;
private ListAdapter mAdapter;
public Elements job;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mListView = (ListView) findViewById(R.id.list);
new NewThread().execute();
mAdapter = new ListAdapter(this, mList);
mListView.setAdapter(mAdapter);
}
public class NewThread extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg) {
Document doc;
try {
doc = (Document) Jsoup.connect("http://www.somewebsite.com")
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get();
title = doc.select("h3.something-to-extract a[href]");
for (Element titles : title) {
mList.add(titles.text()+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
IMO 它与 JSOUP 部分有关,因为当我擦除所有内容 od doInBackground
并只放入
mList.add("Something 1");
mList.add("Something 2");
然后就可以了。请以某种方式帮助我。
编辑:我想解析来自这个 html 片段的数据:
<h2 class="title">
<a href="/jstpl/london/11697582"
title="You just have to wait" class="titles">
Nothing else to say
</a>
我想将“Nothing else to say
”存储到 mList
,例如我的 html 代码中存在的另一个标题。单独的解析部分也很好用。
You have to call
notifyDataSetChanged
on adapter
反映提供给适配器的列表的变化。为此 -
覆盖 NewThread
中的 onPostExecute
并调用 mAdapter.notifyDataSetChanged()
@Override
protected void onPostExecute(String result) {
mAdapter.notifyDataSetChanged();
}
注意: onPostExecute
在主 UI 线程上运行,而不是您的 NewThread
,其中 doInBackground
在内部运行你的NewThread
。 onPostExecute
在后台线程完成处理时调用。现在,因为我们已经用新项目更新了列表。我们将在主线程上通知适配器 运行。希望对你有帮助。