我有一个带有recyclerview的活动,它在活动开始时列出了10封电子邮件(这部分工作正常)。我正在使用asynctask来获取javamail API的数据。此外,由于asynctask是一个单独的类而不是嵌套的,我使用了一个接口来使用recview将onPostExecude委托给我的活动。 但我还需要实现一个函数,如果我在列表的底部,我再次加载10个带有asynctask的电子邮件。 (因为加载它们需要时间,所以一次只有10个)当我在监听器内部时,我遇到了asnyctask委托部分的问题:
Email_InboxActivity类:
public class Email_InboxActivity extends AppCompatActivity implements fetchInboxMail_Interface {
private String email, password, providerName, imapHostName;
private boolean isThereMore = true;
private RecyclerView recList;
private LinearLayoutManager llm;
private fetchInboxMail mAsyncTask;
private fetchInboxMail_Interface mInterface;
public ArrayList<Email_Message> loadedEmails = new ArrayList<Email_Message>();
int firstVisibleItem, visibleItemCount, totalItemCount, startIndex = 0, offset = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email__inbox);
...
//setting up the recyclerview
recList = (RecyclerView) findViewById(R.id.email_list);
recList.setHasFixedSize(true);
llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
recList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = llm.getChildCount();
totalItemCount = llm.getItemCount();
firstVisibleItem = llm.findFirstVisibleItemPosition();
if(isThereMore && totalItemCount == (firstVisibleItem + visibleItemCount) && visibleItemCount > 0 ){
mAsyncTask = new fetchInboxMail(Email_InboxActivity.this, email, password, imapHostName, startIndex, offset);
mAsyncTask.execute();
mAsyncTask.delegate = mInterFace //nullpoint exception, and the keyword "this" also not working since this class doesnt implement the interface
}
}
});
mAsyncTask = new fetchInboxMail(Email_InboxActivity.this, email, password, imapHostName, startIndex, offset);
mAsyncTask.execute();
mAsyncTask.delegate = this;
}
...
public void AsyncTaskFinish(ArrayList<Email_Message> EmailInbox){
loadedEmails.addAll(EmailInbox);
Email_ListAdapter mAdapter = new Email_ListAdapter(loadedEmails);
recList.setAdapter(mAdapter);
startIndex = loadedEmails.size();
SharedPreferences sharedPref = getSharedPreferences("isthere_more_email", Context.MODE_PRIVATE);
isThereMore = sharedPref.getBoolean("isthere_more", true);
if(!isThereMore){
Toast.makeText(Email_InboxActivity.this, getString(R.string.last_email_toast), Toast.LENGTH_LONG).show();
}
}
}
界面:
public interface fetchInboxMail_Interface {
void AsyncTaskFinish(ArrayList<Email_Message> EmailInbox);
}
编辑: asynctask onpostexecute part:
@Override
protected void onPostExecute(ArrayList<Email_Message> EmailInbox) {
super.onPostExecute(EmailInbox);
...
delegate.AsyncTaskFinish(EmailInbox);
}
那么解决方案是什么,所以我也可以使用onScrollListener中的接口?我想创建一个扩展了RecyclerView.OnScrollListener()并实现接口的新类,但问题是我没有在该类中需要的字段(loadedEmails,recList,llm)。
如果anwser是我需要使用asynctask作为嵌套类并删除接口,我会非常难过:(
答案 0 :(得分:1)
您可以更改此行:
mAsyncTask.delegate = mInterFace;
到
mAsyncTask.delegate = Email_InboxActivity.this;
你也可以做
mInterface = this;
在OnCreate方法中,但无论如何,只要你可以使用它来存储对它自己的引用是没有意义的。
希望这有帮助。