AsyncTask 显示加载进度和 return 一个值 jsoup
AsyncTask show loading progress and return a value jsoup
我正在开发一个 Web 应用程序,它将解析站点并将新闻动态加载到 CardView 中。现在它可以工作并完成所有需要的事情。但这并不是我想要的。
这是我的一段代码来理解我在说什么:
public class NewsPage extends ActionBarActivity {
List<NewCard> listNC = new ArrayList<NewCard>();
class NewsParser extends AsyncTask<Void,Void,List<NewCard>> {
Document doc;
List<NewCard> nc = new ArrayList<NewCard>();
ProgressDialog progressDialog;
@Override
protected void onPreExecute()
{
// progressDialog= ProgressDialog.show(NewsPage.this, "Parsing the site", "Please wait while the information is loading...", true);
};
@Override
protected List<NewCard> doInBackground(Void... params) {
try {
//some code skipped
nc.add(new NewCard(forHeader.html(), forDesc, URLforImg, forHeader.attr("href")));
}
} catch (IOException e) {
e.printStackTrace();
}
return nc;
}
protected void onPostExecute(String[] s) {
progressDialog.dismiss();//This method is being called out by new <class name>.execute();
//listNC = new ArrayList<NewCard>(nc);
}
}
在这里我正在检索文章标题以进一步打开。
这是我的 onCreate() 方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_page);
RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
try {
NewsParser np = new NewsParser();
np.execute();
listNC = np.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
NewsAdapter na = new NewsAdapter(listNC);
size = na.sizes;
recList.setAdapter(na);
我正在使用我的适配器用信息填充卡片并处理 onClick 事件。
我的问题是:
- 我需要从我的 AsyncTask 中检索信息以创建一个
卡片列表(在这种情况下,我需要一定数量的卡片)而我不是
确定我可以继续而不返回值。但它让我的应用程序冻结
在操作完成之前不显示任何界面。如何
更好处理?也许我需要让它变得不同?我如何能
单独加载新闻标题(不是全部一起而是按顺序加载)?和
我需要什么样的循环(我不知道如何正确调用它)
在加载新闻时添加新闻(因为如果我的程序无法运行
在做 UI 之前没有列表)?
我已经尝试从我的代码中了解每一个细节,如果需要的话,我也可能会添加我的适配器代码。
您的 UI 正在冻结,因为 try
块中的 get()
方法正在阻塞等待 AsyncTask
完成。这甚至违背了使用 AsyncTask
的目的。相反,在启动 AsyncTask
之前创建 Adapter
,然后在 onPostExecute()
中将适配器的数据设置为新结果并调用适配器的 notifyDataSetChanged()
方法。这将导致 UI 获取更改。
请小心使用 AsyncTask
或任何其他线程机制。它们不是生命周期感知的,因此如果 onPostExecute()
方法对 Activity
或其内部成员字段有任何强引用并尝试直接使用它们,它可能 运行 进入状态异常。
我正在开发一个 Web 应用程序,它将解析站点并将新闻动态加载到 CardView 中。现在它可以工作并完成所有需要的事情。但这并不是我想要的。 这是我的一段代码来理解我在说什么:
public class NewsPage extends ActionBarActivity {
List<NewCard> listNC = new ArrayList<NewCard>();
class NewsParser extends AsyncTask<Void,Void,List<NewCard>> {
Document doc;
List<NewCard> nc = new ArrayList<NewCard>();
ProgressDialog progressDialog;
@Override
protected void onPreExecute()
{
// progressDialog= ProgressDialog.show(NewsPage.this, "Parsing the site", "Please wait while the information is loading...", true);
};
@Override
protected List<NewCard> doInBackground(Void... params) {
try {
//some code skipped
nc.add(new NewCard(forHeader.html(), forDesc, URLforImg, forHeader.attr("href")));
}
} catch (IOException e) {
e.printStackTrace();
}
return nc;
}
protected void onPostExecute(String[] s) {
progressDialog.dismiss();//This method is being called out by new <class name>.execute();
//listNC = new ArrayList<NewCard>(nc);
}
}
在这里我正在检索文章标题以进一步打开。
这是我的 onCreate() 方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_page);
RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
try {
NewsParser np = new NewsParser();
np.execute();
listNC = np.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
NewsAdapter na = new NewsAdapter(listNC);
size = na.sizes;
recList.setAdapter(na);
我正在使用我的适配器用信息填充卡片并处理 onClick 事件。
我的问题是:
- 我需要从我的 AsyncTask 中检索信息以创建一个 卡片列表(在这种情况下,我需要一定数量的卡片)而我不是 确定我可以继续而不返回值。但它让我的应用程序冻结 在操作完成之前不显示任何界面。如何 更好处理?也许我需要让它变得不同?我如何能 单独加载新闻标题(不是全部一起而是按顺序加载)?和 我需要什么样的循环(我不知道如何正确调用它) 在加载新闻时添加新闻(因为如果我的程序无法运行 在做 UI 之前没有列表)?
我已经尝试从我的代码中了解每一个细节,如果需要的话,我也可能会添加我的适配器代码。
您的 UI 正在冻结,因为 try
块中的 get()
方法正在阻塞等待 AsyncTask
完成。这甚至违背了使用 AsyncTask
的目的。相反,在启动 AsyncTask
之前创建 Adapter
,然后在 onPostExecute()
中将适配器的数据设置为新结果并调用适配器的 notifyDataSetChanged()
方法。这将导致 UI 获取更改。
请小心使用 AsyncTask
或任何其他线程机制。它们不是生命周期感知的,因此如果 onPostExecute()
方法对 Activity
或其内部成员字段有任何强引用并尝试直接使用它们,它可能 运行 进入状态异常。