如何从适配器 class 调用 AsyncTask?
How to Call AsyncTask from Adapter class?
我有 activity class 和 GetMembersAsyncTask
。我还有一个适配器 class 和 DeleteAsynccTask
。当我单击列表视图项目中的取消图像时,DeleteAsynccTask
将调用。如果该项目删除成功,那么我将在 DeleteAsynccTask
中的 OnPostExecute()
方法中调用 GetMemberAsynacTask
.
public class AddmemberSearch extends Activity {
private ListViewAdapter listviewAdapter;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.family_search_members);
ButterKnife.inject(this);
new GetFamilyListAsynctask().execute();
}
class GetMembersAsyncTask extends AsyncTask<String, String, String> {
protected void OnPreExecute(){}
protected String doInBackground(String... params) {
some code here
}
protected void onPostExecute(String file_url) { }
}
}
现在ListViewAdapter
class是这样的
public class ListViewAdapter extends BaseAdapter {
Activity context;
private ImageButton cancel_img;
SharedPreferences sharedPrefUser;
public ListViewAdapter(Activity context, ArrayList<familiesListBO> data) {
super();
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView txtViewMember;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
FamiliesListBO familiesListBO;
familiesListBO = data.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.added_member_listview, null);
holder = new ViewHolder();
holder.txtViewMember = (TextView) convertView.findViewById(R.id.txt_addedmember_names);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (familiesListBO.getName() != null) {
holder.txtViewMember.setText(familiesListBO.getName());
familiesListBO.getName();
}
cancel_img = (ImageButton) convertView.findViewById(R.id.cancel_row_img);
cancel_img.setTag(holder);
cancel_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
new DeleteAsynccTask().execute();
}
});
return convertView;
}
class DeleteAsynccTask extends AsyncTask<String, String, String> {
ResultVO getDeletionsResponse;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
getDeletionsResponse = services.delete("this is delete code");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
int appStatusCode = getDeletionsResponse.getAppStatusCode();
if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_SUCCESS_TITLE, IFamilyUtil.FAMILY_DELETE_SUCCESS_MESSAGE, true);
new AddmemberSearch.GetMembersAsyncTask().execute();
} else {
ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_FAIL_TITLE, IFamilyUtil.FAMILY_DELETE_FAIL_MESSAGE, true);
}
notifyDataSetChanged();
}
}
}
此处如何从适配器 class 调用 GetMemberAsynctask
?
您可以将适配器 class 移到 activity class 中,并将适配器 class 设置为内部 class.so 您可以在适配器中调用 GetMemberAsynctask像这样:
public class AddmemberSearch extends Activity {
private ListViewAdapter listviewAdapter;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.family_search_members);
ButterKnife.inject(this);
new GetFamilyListAsynctask().execute();
}
public class ListViewAdapter extends BaseAdapter {
Activity context;
private ImageButton cancel_img;
SharedPreferences sharedPrefUser;
public ListViewAdapter(Activity context, ArrayList<familiesListBO> data) {
super();
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView txtViewMember;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
FamiliesListBO familiesListBO;
familiesListBO = data.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.added_member_listview, null);
holder = new ViewHolder();
holder.txtViewMember = (TextView) convertView.findViewById(R.id.txt_addedmember_names);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (familiesListBO.getName() != null) {
holder.txtViewMember.setText(familiesListBO.getName());
familiesListBO.getName();
}
cancel_img = (ImageButton) convertView.findViewById(R.id.cancel_row_img);
cancel_img.setTag(holder);
cancel_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
new DeleteAsynccTask().execute();
}
});
return convertView;
}
class DeleteAsynccTask extends AsyncTask<String, String, String> {
ResultVO getDeletionsResponse;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
getDeletionsResponse = services.delete("this is delete code");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
int appStatusCode = getDeletionsResponse.getAppStatusCode();
if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_SUCCESS_TITLE, IFamilyUtil.FAMILY_DELETE_SUCCESS_MESSAGE, true);
new AddmemberSearch.GetMembersAsyncTask().execute();
} else {
ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_FAIL_TITLE, IFamilyUtil.FAMILY_DELETE_FAIL_MESSAGE, true);
}
notifyDataSetChanged();
}
}
class GetMembersAsyncTask extends AsyncTask<String, String, String> {
protected void OnPreExecute(){}
protected String doInBackground(String... params) {
some code here
}
protected void onPostExecute(String file_url) { }
}
希望对您有所帮助!
只需将 ListViewAdapter 放入 AddMemberSearch class
从 AddmemberSearch class
中删除 GetMembersAsyncTask 内部 class
和
将 GetMembersAsyncTask 作为单独的 class(将其放入您的包中)
之后你可以从任何地方这样调用"new GetFamilyListAsynctask().execute();"。
我有 activity class 和 GetMembersAsyncTask
。我还有一个适配器 class 和 DeleteAsynccTask
。当我单击列表视图项目中的取消图像时,DeleteAsynccTask
将调用。如果该项目删除成功,那么我将在 DeleteAsynccTask
中的 OnPostExecute()
方法中调用 GetMemberAsynacTask
.
public class AddmemberSearch extends Activity {
private ListViewAdapter listviewAdapter;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.family_search_members);
ButterKnife.inject(this);
new GetFamilyListAsynctask().execute();
}
class GetMembersAsyncTask extends AsyncTask<String, String, String> {
protected void OnPreExecute(){}
protected String doInBackground(String... params) {
some code here
}
protected void onPostExecute(String file_url) { }
}
}
现在ListViewAdapter
class是这样的
public class ListViewAdapter extends BaseAdapter {
Activity context;
private ImageButton cancel_img;
SharedPreferences sharedPrefUser;
public ListViewAdapter(Activity context, ArrayList<familiesListBO> data) {
super();
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView txtViewMember;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
FamiliesListBO familiesListBO;
familiesListBO = data.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.added_member_listview, null);
holder = new ViewHolder();
holder.txtViewMember = (TextView) convertView.findViewById(R.id.txt_addedmember_names);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (familiesListBO.getName() != null) {
holder.txtViewMember.setText(familiesListBO.getName());
familiesListBO.getName();
}
cancel_img = (ImageButton) convertView.findViewById(R.id.cancel_row_img);
cancel_img.setTag(holder);
cancel_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
new DeleteAsynccTask().execute();
}
});
return convertView;
}
class DeleteAsynccTask extends AsyncTask<String, String, String> {
ResultVO getDeletionsResponse;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
getDeletionsResponse = services.delete("this is delete code");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
int appStatusCode = getDeletionsResponse.getAppStatusCode();
if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_SUCCESS_TITLE, IFamilyUtil.FAMILY_DELETE_SUCCESS_MESSAGE, true);
new AddmemberSearch.GetMembersAsyncTask().execute();
} else {
ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_FAIL_TITLE, IFamilyUtil.FAMILY_DELETE_FAIL_MESSAGE, true);
}
notifyDataSetChanged();
}
}
}
此处如何从适配器 class 调用 GetMemberAsynctask
?
您可以将适配器 class 移到 activity class 中,并将适配器 class 设置为内部 class.so 您可以在适配器中调用 GetMemberAsynctask像这样:
public class AddmemberSearch extends Activity {
private ListViewAdapter listviewAdapter;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.family_search_members);
ButterKnife.inject(this);
new GetFamilyListAsynctask().execute();
}
public class ListViewAdapter extends BaseAdapter {
Activity context;
private ImageButton cancel_img;
SharedPreferences sharedPrefUser;
public ListViewAdapter(Activity context, ArrayList<familiesListBO> data) {
super();
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView txtViewMember;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
FamiliesListBO familiesListBO;
familiesListBO = data.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.added_member_listview, null);
holder = new ViewHolder();
holder.txtViewMember = (TextView) convertView.findViewById(R.id.txt_addedmember_names);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (familiesListBO.getName() != null) {
holder.txtViewMember.setText(familiesListBO.getName());
familiesListBO.getName();
}
cancel_img = (ImageButton) convertView.findViewById(R.id.cancel_row_img);
cancel_img.setTag(holder);
cancel_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
new DeleteAsynccTask().execute();
}
});
return convertView;
}
class DeleteAsynccTask extends AsyncTask<String, String, String> {
ResultVO getDeletionsResponse;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
getDeletionsResponse = services.delete("this is delete code");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
int appStatusCode = getDeletionsResponse.getAppStatusCode();
if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_SUCCESS_TITLE, IFamilyUtil.FAMILY_DELETE_SUCCESS_MESSAGE, true);
new AddmemberSearch.GetMembersAsyncTask().execute();
} else {
ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_FAIL_TITLE, IFamilyUtil.FAMILY_DELETE_FAIL_MESSAGE, true);
}
notifyDataSetChanged();
}
}
class GetMembersAsyncTask extends AsyncTask<String, String, String> {
protected void OnPreExecute(){}
protected String doInBackground(String... params) {
some code here
}
protected void onPostExecute(String file_url) { }
}
希望对您有所帮助!
只需将 ListViewAdapter 放入 AddMemberSearch class
从 AddmemberSearch class
中删除 GetMembersAsyncTask 内部 class
和
将 GetMembersAsyncTask 作为单独的 class(将其放入您的包中)
之后你可以从任何地方这样调用"new GetFamilyListAsynctask().execute();"。