我试图通过从网站上删除数据来填充 RecyclerView 。问题是getData()
{model}的SearchCards.java
方法在 AsyncTask 完成执行之前返回String(pageTitle),导致空String。这是我的代码
FragmentOne.java
mAdapter = new SearchAdapter(SearchCards.getData(),this,recyclerView);
recyclerView.setAdapter(mAdapter);
SearchCards.java
public static ArrayList<SearchCards> getData() {
ArrayList<SearchCards> datatList = new ArrayList<SearchCards>();
String pageTitle = new Scrapping().scrapSubs(); //scrapSubs contains AsynTask
String[] yearArr = setYearArr();
for (int i = 0; i < yearArr.length; i++) {
SearchCards searchCard = new SearchCards();
searchCard.setYear(yearArr[i]);
searchCard.setTitle(pageTitle); //same pagetitle for every year
datatList.add(searchCard);
}
return dataList;
}
Scrapping.java
public String ScrapSubs(){
JsoupAsyncTask jsoupAsyncTask = new JsoupAsyncTask();
jsoupAsyncTask.execute();
return pageTitle;
}
private class JsoupAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
htmlDocument = Jsoup.connect(htmlPageUrl).get();
pageTitle = htmlDocument.title();
Log.i(TAG,"PAGETITLE"+pageTitle);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
//do something
}
}
如何在Scrapping.java中执行返回pageTitle之类的操作仅在AsyncTask执行完成后执行。我尝试了这个但是没有用:
if(jsoupAsyncTask.getStatus() == AsyncTask.Status.FINISHED){
return pageTitle;
}
修改 可能这不是一个研究得很好的问题。在这里,我找到了我要找的东西:http://stacktips.com/tutorials/android/android-recyclerview-example 我所要做的就是在onPostExecute()中移动SearchAdapter初始化。
答案 0 :(得分:1)
private class JsoupAsyncTask extends AsyncTask<Void, Void, You Result Data Type> {
@Override
protected You Result Data Type doInBackground(Void... params) {
try {
htmlDocument = Jsoup.connect(htmlPageUrl).get();
pageTitle = htmlDocument.title();
Log.i(TAG,"PAGETITLE"+pageTitle);
} catch (IOException e) {
e.printStackTrace();
}
return Your Execution Result;
}
@Override
protected void onPostExecute(You Result Data Type Your Execution Result) {
//Do Rest of Your Program
}
}
答案 1 :(得分:1)
创建intarface:
private class JsoupAsyncTask extends AsyncTask<Void, Void, Void> {
public interface FinishListener {
void processFinish(Boolean isFinish);
}
FinishListener finishListener = null;
public JsoupAsyncTask(FinishListener finishListener) {
this.finishListener = finishListener;
}
@Override
protected Void doInBackground(Void... params) {
try {
htmlDocument = Jsoup.connect(htmlPageUrl).get();
pageTitle = htmlDocument.title();
Log.i(TAG, "PAGETITLE" + pageTitle);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
finishListener.processFinish(true);
//do something
}
}
听众:
new JsoupAsyncTask(new JsoupAsyncTask.FinishListener() {
@Override
public void processFinish(Boolean isFinish) {
// do somthing }
}).execute();
答案 2 :(得分:0)
如果您希望代码同步运行,那么您根本不需要使用AsyncTask - 只需创建一个可以满足您需要的方法并调用它。 然而,这在UI线程中不起作用 - UI线程方法必须快速返回,否则整个应用程序停止。 您可能需要重新考虑您的设计。一种方法是显示某种“等待ui”,微调器,进度条等,而AsyncTask在后台运行,只有当你有数据移动并显示它时。