我有一个活动的回收视图,其中列出10封电子邮件时,活动开始(这部分工作很好)。我使用异步任务使用javamail获取数据。另外,由于异步任务位于单独的类中,而不是嵌套的,所以我使用一个接口将onPostExecude委托给我的活动和recview。但是我还需要实现一个函数,如果我在列表的底部,我会再次加载10封包含异步任务的电子邮件。(一次只需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);
}编辑:后执行部分上的异步任务:
@Override
protected void onPostExecute(ArrayList<Email_Message> EmailInbox) {
super.onPostExecute(EmailInbox);
...
delegate.AsyncTaskFinish(EmailInbox);
}那么,解决方案是什么,这样我也可以在onScrollListener中使用这个接口?我考虑创建一个扩展RecyclerView.OnScrollListener()并实现接口的新类,但问题是在该类中没有我需要的字段(loadedEmails、recList、llm)。
如果anwser认为我需要使用异步任务作为嵌套类并删除接口,我将感到非常难过:(
发布于 2015-10-08 10:25:08
您可以更改这一行:
mAsyncTask.delegate = mInterFace;至
mAsyncTask.delegate = Email_InboxActivity.this;你也可以
mInterface = this;在OnCreate方法中,但是无论如何,将引用存储到您只可以使用它的地方是没有意义的。
希望这能有所帮助。
https://stackoverflow.com/questions/33012421
复制相似问题