如何从Async类调用适配器的刷新方法
我想在适配器的方法refreshlikes()中做一些事情,所以我必须从Async类调用此方法。那么如何获取适配器的运行对象然后调用适配器的方法。
public class HomeAdapter extends RecyclerView.Adapter<HomeRowHolder> implements
OnClickListener {
ArrayList<PostModel> postlist = new ArrayList<PostModel>();
Context context;
HomeAdapter adapter;
FragmentManager mFragmentManager;
HomeRowHolder myhomeRowHolder;
public HomeAdapter(FragmentManager mFragmentManager,
ArrayList<PostModel> postlist, Context c) {
this.postlist = postlist;
this.context = c;
this.mFragmentManager = mFragmentManager;
Log.e("mFragmentManager", "" + mFragmentManager);
// TODO Auto-generated constructor stub
}
@Override
public HomeRowHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.post_layout, null);
HomeRowHolder mh = new HomeRowHolder(v);
return mh;
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return (null != postlist ? postlist.size() : 0);
}
@Override
public void onBindViewHolder(HomeRowHolder homerowholder, int i) {
// TODO Auto-generated method stub
PostModel postmodel = postlist.get(i);
homerowholder.likes.setText(postmodel.getLikes());
homerowholder.dislikes.setText(postmodel.getDislikes());
homerowholder.comments.setText(postmodel.getComment());
homerowholder.time.setText(postmodel.getTime());
homerowholder.post.setText(postmodel.getPost());
setlisteners(homerowholder, i);
}
private void setlisteners(final HomeRowHolder homerowholder,
final int position) {
// TODO Auto-generated method stub
homerowholder.likes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// (HomeAdapter)context;
new GetLikesTask(mFragmentManager, context, homerowholder,
position).execute();
//getdata().execute()
}
});
homerowholder.dislikes.setOnClickListener(this);
homerowholder.comments.setOnClickListener(this);
homerowholder.post.setOnClickListener(this);
}
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.like_image) {
}
if (v.getId() == R.id.report_image) {
}
if (v.getId() == R.id.comment_image) {
}
if (v.getId() == R.id.post) {
Log.e("activiyty exist in home adapter", ""
+ !new AllPostActivity().isDestroyed());
((AllPostActivity) context).pushFragment(new SinglePostFragment());
}
}
public void refreshlikes() {
// "here i will notify this"
}
public class GetLikesTask extends AsyncTask<String, Void, String> {
Context c;
HomeRowHolder homerowholder;
FragmentManager fragmentmanger;
public GetLikesTask(FragmentManager fragmentmanger,Context c,HomeRowHolder homerowholder, int position ) {
// TODO Auto-generated constructor stub
this.fragmentmanger=fragmentmanger;
this.c=c;
this.homerowholder=homerowholder;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
homerowholder.likes.setText("212");
((HomeAdapter) ).refreshlikes();`
}
答案 0 :(得分:3)
您可以将回调抛出传递给AsyncTask构造函数,并在onPostExecute()中运行此回调的方法。
回调的示例代码:
public interface IRefreshCallback {
void onRefreshAdapter();
}`
初始化AsyncTask的代码应如下所示:
new GetLikesTaskfragmentmanger, context, homerowholder, position,
new IContactCallback(){
@Override
void onRefreshAdapter(){
//Or other method
adapter.refresh();
}
});
你的AsyncTask应该是这样的:
public class GetLikesTask extends AsyncTask<String, Void, String> {
IRefreshCallback callback;
Context c;
HomeRowHolder homerowholder;
FragmentManager fragmentmanger;
public GetLikesTask(FragmentManager fragmentmanger,Context c,HomeRowHolder homerowholder, int position, IRefreshCallback callback ) {
// TODO Auto-generated constructor stub
this.callback = callback
this.fragmentmanger=fragmentmanger;
this.c=c;
this.homerowholder=homerowholder;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
homerowholder.likes.setText("212");
((HomeAdapter) ).refreshlikes();`
this.callback.refresh();
}
同样,您也可以将AsyncTask的结果传递给rowHolders。无需将 FragmentManager 或 HomeRowHolder 等整个对象传递给AsyncTask。