How to Call AsyncTask from Adapter class?

末鹿安然 提交于 2020-01-06 20:24:15

问题


i have activity class with GetMembersAsyncTask. Also i have one Adapter class with DeleteAsynccTask . When i click cancel image in list View item then DeleteAsynccTask will call. If that item deleted successfully then i will call GetMemberAsynacTask inside OnPostExecute() method in DeleteAsynccTask .

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) { }
}
}

Now ListViewAdapter class is like this.

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();           
    }
}
}

Here How to Call GetMemberAsynctask from Adapter class?


回答1:


You can move your adapter class inside your activity class and make adapter class as inner class.so you have access for calling GetMemberAsynctask in adapter like this:

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) { }
}

I hope it helps!




回答2:


Remove GetMembersAsyncTask Inner class from AddmemberSearch class
and
Make GetMembersAsyncTask as a seprate class (put it in your package)

After that you can call like this "new GetFamilyListAsynctask().execute();" from any where.




回答3:


Simply put the ListViewAdapter inside the AddMemberSearch class



来源:https://stackoverflow.com/questions/32068907/how-to-call-asynctask-from-adapter-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!